NSUserScriptTask: accepted file types?

随声附和 提交于 2019-12-10 21:36:16

问题


I'm using NSUserScriptTask to run scripts the user placed in the Application Scripts directory. However, I'm finding it hard to find out which are the known types of scripts that NSUserScriptTask accepts and the docs don't help much either... Any ideas?

So far I've tried these:

  • .sh
  • .applescript
  • .scpt
  • .scptd
  • .txt

And coudn't get any of them to work (the initWithURL:error: method returns nil)

EDIT: I forgot to include the error! Silly me... Here's what gets printed on the log:

Error Domain=NSCocoaErrorDomain Code=259 "The file “Untitled.applescript” couldn’t be opened because it isn’t in the correct format." UserInfo=0x100121f50 {NSURL=file://localhost/Users/path to file/Untitled.applescript, NSFilePath=/Users/path to file/Untitled.applescript}

回答1:


The documentation for NSUserScriptTask claims that instantiating that class directly will work on any valid file and return the appropriate kind of task:

The returned object will be of one of the specific sub-classes (NSUserUnixTask, NSUserAppleScriptTask, and NSUserAutomatorTask), or nil if the file does not appear to match any of the known types.

If invoked from a subclass, the result will be that class or nil.

In reality, I found that (as of 10.8.2) NSUserScriptTask unconditionally returns nil and a “what is this i dont even” error. It seems that you need to instantiate the correct task subclass yourself. Probably worth filing a bug.

To test whether a file is usable as a script task (e.g., in an Open panel validation method), all I can suggest is to try instantiating each of the three classes, returning YES if any of them succeeds and NO if all of them fail.



来源:https://stackoverflow.com/questions/15603062/nsuserscripttask-accepted-file-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!