How do I save a file created by OS X Dashboard widgets?
I have a web app that I am porting to an OS X Dashboard widget. The web app generates some client-side data using JavaScript, and then when the user wants to save it, it sends it to the server-side script that passes the data back with an Content-disposition: attachment;
HTTP header , causing the save -file to be saved in most browsers.
I tried to duplicate this in the Dashboard environment. The widget just disappears. I don't know, but I suspect it might actually jump to unfamiliar content.
So how do I initiate the "save file dialog" - or, otherwise, use any method at all to save the data the Widget generates as a file?
a source to share
As far as I know, it is not possible to save a file from a toolbar widget via the "save file" dialog, since the purpose of widgets is to display information (with or without Internet access), perform calculations, or control applications.
However, you have three options for storing data locally:
-
Save the data in the widget settings. Set the data using the setPreferenceForKey widget method and retrieve it using the preferenceForKey . This is only an option if the data belongs to the widget and should not be available outside the widget. Also, the data size should not be too large.
-
Execute scripts using the system command of the widget. Any scripting language (sh, perl, ruby, python, AppleScript, ...) can be used here.
-
Write a Cocoa / Objective-C widget plugin .
a source to share
The correct way to do this is the cocoa widget plugin, which will allow you to use the save dialog. A quick way to do it something like this:
command = widget.system("/bin/bash -c 'cat - > ~/Desktop/test.txt'", yourHandler);
command.write( "some text" );
command.close();
This tells bash to cat stdin to a file on the desktop and then writes to stdin.
a source to share