NSTask with Multiple Path Parameters

The following script is intended to open files with the qlmanage utility, but it doesn’t work:

use framework "Foundation"
use scripting additions

set ssFolder to "/Users/Robert/Screenshots/" -- change to folder with image files
set ssFolder to current application's |NSURL|'s fileURLWithPath:ssFolder
set fileManager to current application's NSFileManager's defaultManager()
set folderContents to fileManager's contentsOfDirectoryAtURL:(ssFolder) includingPropertiesForKeys:{} options:4 |error|:(missing value)
set ssFiles to ((folderContents's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:")
set ssFiles to ssFiles's componentsJoinedByString:"' '"
set ssFiles to current application's NSString's stringWithFormat_("'%@'", ssFiles)

--this doesn't work
set commandPath to "/usr/bin/qlmanage"
set commandArguments to {"-p", ssFiles}
set theTask to current application's NSTask's new()
theTask's setLaunchPath:commandPath
theTask's setArguments:commandArguments
set theResult to theTask's launchAndReturnError:(missing value)

--this works
--do shell script "/usr/bin/qlmanage -p " & (ssFiles as text)

The reason it doesn’t work is because each parameter has to be a functional unit (a file path in this instance). I say this because the following works:

use framework "Foundation"
use scripting additions

set ssFileOne to "/Users/Robert/Screenshots/aa aa.png"
set ssFileTwo to "/Users/Robert/Screenshots/bb bb.png"

set commandPath to "/usr/bin/qlmanage"
set commandArguments to {"-p", ssFileOne, ssFileTwo}
set theTask to current application's NSTask's new()
theTask's setLaunchPath:commandPath
theTask's setArguments:commandArguments
set theResult to theTask's launchAndReturnError:(missing value)

Thanks for any suggestions.

It’s less complicated than you think.

Unlike do shell script you don’t need to escape the paths because each item in the argument array is treated individually. Spaces in paths don’t matter.

As you can see in the second example the arguments parameter is an array of strings. You joined the array to a single string which cannot work.

Just append the array to the first -p item

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set ssFolder to  "/Users/Robert/Screenshots/" -- change to folder with image files
set ssFolder to current application's |NSURL|'s fileURLWithPath:ssFolder
set fileManager to current application's NSFileManager's defaultManager()
set folderContents to fileManager's contentsOfDirectoryAtURL:(ssFolder) includingPropertiesForKeys:{} options:4 |error|:(missing value)
set ssFiles to ((folderContents's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:")

set commandPath to "/usr/bin/qlmanage"
set commandArguments to {"-p"} & ssFiles
set theTask to current application's NSTask's new()
theTask's setLaunchPath:commandPath
theTask's setArguments:commandArguments
set theResult to theTask's launchAndReturnError:(missing value)
1 Like

Thanks Stefan. That works great.