Search code examples
applescript

Setting list of files as alias to POSIX


I'm new to applescript and have been trying to select multiple files (or a folder if it's easier) and return the path in posix file format. With the end goal of running a vba macro on each file selected. All of the answers that seem to have dealt with this are from when macscript wasn't deprecated. I have gotten the below to return as a POSIX file when only one file is selected, otherwise only one path is shown. I tried to get around that by incrementally adding each change to a list, but that seems to break the conversion to POSIX.

on select_files()
    set filelist to {}
    set CSV_files to choose file with prompt "Pick CSV Files to convert" with multiple selections allowed
    
    --tell application "System Events"
    --set C_path to POSIX path of CSV_files
    --end tell
    
    repeat with a in CSV_files
        convertPathToPOSIXString(a)
        --InsertItemInList(a, filelist, -1)
    end repeat
end select_files

select_files()


on convertPathToPOSIXString(thePath)
    tell application "System Events"
        try
            set thePath to path of disk item (thePath as string)
        on error
            set thePath to path of thePath
        end try
    end tell
    return POSIX path of thePath
end convertPathToPOSIXString

I've edited out other methods I've tried that haven't worked. Both convertPathToPOSIXString and InsertItemInList were taken from the mac automation scripting guide.


Solution

  • System Events is not needed. You can get the POSIX path of an alias specifier directly

    Append each POSIX path to filelist and return filelist

    on select_files()
        set filelist to {}
        set CSV_files to choose file with prompt "Pick CSV Files to convert" with multiple selections allowed
        
        repeat with a in CSV_files
            set end of filelist to POSIX path of a
        end repeat
        return filelist
    end select_files
    
    select_files()