Passing an array of FolderItems to an AppleScript

I want to use the Finder to move a bunch of files that the user has selected in my listbox. So I can’t really know how many it’ll be. Could be one, could be thousands.

How would I efficiently pass a list of file references to an AppleScript using Xojo?

I can think of only one: Concatenate all paths (FolderItem.AbsolutePath) into a String, separating them with a control char such as Tab or something else that’s unlikely to appear in a file name, and then pass that String to the script, which disassembles them back into a list.

Has any done this, do you have tested AppleScript for that?

Check NSAppleScript classes:

The NSAppleEventDescriptorMBS class allows you to build lists and stuff Folderitems inside:

I send ShellPath to AppleScript instead of AbsolutePath. But Xojo ShellPath use \ before some characters like “\ “, “\:”, “\”, “\’”,””"
Then I have a Method I apply to Xojo ShellPath before send them to AppleScript. I send “CetElt.ShellPath” which contain Xojo ShellPath :

TampTextA = ReplaceAll(CetElt.ShellPath, "\\\", "::") ' Because impossible to have 2 ':' TampTextA = ReplaceAll(TampTextA, "\", "") ' Remove all \\ TampTextA = ReplaceAll(TampTextA, "::", "\") ' except \\\\
TampTextA contains my AppleScript ShellPath.

Do that for all yours paths and separate them with a Tab and send the String to AppleScript and make the Tab spliting Tab.
In AppleScript, I do

set item1PosIX to (POSIX file item1T) as item

item1T is TampTextA

Thomas, do you know the AppleScript code to split up the text? That’s what I was struggling with.

Also, that replace of yours is not safe, I believe. Mac file names may very well contain “/” or “//”, which would be turned into “:” and “::” for a shell path, I believe. If you need a safe tmp replacement string, better use Chr(1) or Chr(0) - they may also appear in a file name, but that’s at least something a user cannot enter - while they can easily enter a “/” into a file name using the Finder.

Also, intead of using ShellPath, you should use NativePath. Then you do not need to perform the conversion. Also, AbsolutePath is still safe on OS X, and AppleScript will handle them just fine - no need for “posix file”.

I ended up using the MBS code, as that felt safer and more efficient to me. The code looks like this:

[code]Private Function moveFiles(sourceFiles() as FolderItem, destFolder as FolderItem) As Boolean
dim script as new NSAppleScriptMBS (moveScriptCode)
dim error as Dictionary
if not script.Compile (error) then

dim srcList as NSAppleEventDescriptorMBS = NSAppleEventDescriptorMBS.listDescriptor()
for each f as FolderItem in sourceFiles
srcList.insertDescriptor NSAppleEventDescriptorMBS.descriptorWithFSRef (f), 0

dim args() As NSAppleEventDescriptorMBS
args.Append srcList
args.Append NSAppleEventDescriptorMBS.descriptorWithFSRef (destFolder)

dim p as NSAppleEventDescriptorMBS = script.executeSubroutine (“moveFiles”, args, error)
if error <> nil then

End Function[/code]

And the AppleScript, stored in a Constant named “moveScriptCode”:

on moveFiles (src, dest) tell application "Finder" with timeout of 5 seconds move src to dest end timeout end tell end moveFiles

[quote=319677:@Thomas Tempelmann]Thomas, do you know the AppleScript code to split up the text? That’s what I was struggling with.
Applescripts text item delimiters
save the current set
switch them to whatever it is you want
split the thing up using the delimiters
set them back to normal

Sorry, I was on Holliday.
If it works with PBS plugins it’s perfect. Here is an exemple of make a Tab from string in AppleScript (like the Xojo Split function) :

set AppleScript's text item delimiters to (MySeparatorText) -- Tabulation or what you want set the MyTabText to every text item of the TextToSplit set AppleScript's text item delimiters to "" -- Reset to default