简体   繁体   English

如何保存由OS X仪表板小部件生成的文件?

[英]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: 但是,您有三个选项可以在本地存储数据:

  1. 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. 同样,数据大小也不能太大。

  2. 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等)。

  3. 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.

相关问题 如何让浏览器提示用户保存服务器动态生成的文件? - How do I get browser to prompt user to save a file dynamically generated by server? 如何在 Yosemite OS X 的 Safari 8 中复制到剪贴板? - How do I copy to clipboard in Safari 8 on Yosemite OS X? 如何在 OS X 上安装 Javascript 运行时解释器? - How do I get a Javascript runtime interpreter installed on OS X? 如何在 javascript 上保存文件 url? - How do I save file urls on javascript? 如何在文件中将JavaScript生成的内容另存为HTML? - How to save JavaScript generated content as HTML in the file? 如何为使用JSP生成的下载文件显示“保存文件”对话框? - How can I present a “save file” dialog box for a downloaded file generated using JSP? 如何完全卸载 Node.js,并从头开始重新安装 (Mac OS X) - How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) 如何将生成的 svg 代码保存到客户端计算机中的 .svg 文件中? - How can I save my generated svg code to a .svg file in the client machine? 如何用heatmap.js保存canvas标签生成的图像文件? - How I can save the image file generated by canvas tag with heatmap.js? 我如何*本地*保存由javascript生成的.html文件(在* local * .html页面上运行)? - How can I *locally* save an .html file generated by javascript (running on a *local* .html page)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM