[英]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. 我有一个要移植到OS X仪表板小部件的Web应用程序。 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;
该Web应用程序使用JavaScript在客户端生成一些数据,然后,当用户想要保存数据时,将其发送到服务器端脚本,该脚本将数据中继回
Content-disposition: attachment;
HTTP header, triggering a save-file dialog in most browsers. HTTP标头,在大多数浏览器中触发保存文件对话框。
I've tried duplicating this in the Dashboard environment. 我尝试在Dashboard环境中复制此内容。 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? 因此,如何触发“保存文件对话框”-否则,将根本不使用任何方法将Widget生成的数据保存为文件?
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. 据我所知,不可能通过“保存文件”对话框从仪表板小部件中保存文件,因为小部件的目的是显示信息(有或没有Internet访问权限),进行计算或控制应用程序。
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.
通过setPreferenceForKey小部件方法设置数据,然后使用preferenceForKey小部件方法检索数据。 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, ...).
此处可以使用任何脚本语言(sh,perl,ruby,python,AppleScript等)。
Write a Cocoa/Objective-C based widget plugin . 编写一个基于Cocoa / Objective-C的小部件插件 。
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. 这告诉bash将stdin转移到桌面上的文件,然后写入stdin。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.