3

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}
Alex
  • 5,009
  • 3
  • 39
  • 73

1 Answers1

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Excellent! That did the trick, except we now have to find which file types are accepted... I found these are: .applescript .scpt .scptd .workflow, but .sh isn't accepted by NSUserUnixTask and I have no idea what is... – Alex Mar 24 '13 at 23:24
  • @Alex: See my last paragraph. – Peter Hosey Mar 24 '13 at 23:25
  • Yes, I know, but I'd like to tell the user beforehand which file types are supported, and I just wanted to know if you had any ideas on that. I'll be using your method to test the files programmatically, of course. – Alex Mar 24 '13 at 23:34
  • @Alex: For AppleScript and Automator scripts, that's easy. For shell scripts, it might simply accept anything that you have permission to execute that either is a Mach-O executable or has a shebang, in which case you might need to fib a little and suggest a much smaller list of your own devising (e.g., “.sh, .command, .py, .rb”) while doing the real test in your Open panel delegate. – Peter Hosey Mar 25 '13 at 02:14
  • So far in my tests it doesn't seem to accept files with a shebang either. Haven't tried Mach-O files yet. Maybe this class is as bugged as NSUserScriptTask? – Alex Mar 25 '13 at 08:46
  • @Alex: It worked for me. Did you set the necessary execute permissions on the script? – Peter Hosey Mar 25 '13 at 08:47
  • 1
    I get an NSUserUnixTask object for a file ending in sh providing the file has been marked as executable (chmod + x) – Confused Vorlon Oct 10 '14 at 21:59
  • @ConfusedVorlon: Sounds like it's fixed, then. Thanks for the update. – Peter Hosey Oct 12 '14 at 18:25