Question

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}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top