How do I save a file generated by an OS X Dashboard Widget?
I've got a web app that I'm porting to an OS X Dashboard widget. The web app generates some data on the client side using JavaScript, and then, when the user wants to save it, sends it to a server-side script which relays the data back with the Content-disposition: attachment;
HTTP header, triggering a save-file dialog in most browsers.
I've tried duplicating this in the Dashboard environment. The Widget simply disappears. I don't know, but I suspect that it may be actually navigating to the unfamiliar content.
开发者_如何学编程So, how do I trigger a "save file dialog" -- or, failing that, use any method at all to save the data the Widget generates as a file?
As far as I know it is not possible to save a file from a dashboard widget through a "save file" dialog because the intent of widgets is to display information (with or without internet access), to do calculations or to control applications.
However you have three options to store data locally:
Store the data in the preferences of the widget. Set the data through the setPreferenceForKey widget method and retrieve it with the preferenceForKey widget method. This is only an option if the data belongs to the widget and does not have to be accessible outside the widget. Also the data size should not be too big.
Execute scripts through the system command of the widget. Any scripting language can be used here (sh, perl, ruby, python, AppleScript, ...).
Write a Cocoa/Objective-C based widget plugin.
The right way to do it is a cocoa widget plugin that will let you use a save as dialog. The quick way to do it is 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, then writes to stdin.
精彩评论