Search code examples
pathapplescriptfilenamesadobe-illustrator

How to specify collections of files and folders for processing with Applescript?


I'm not very skilled at AppleScript and am completely confused about the various ways to name and enumerate files and paths. I've tried just about everything I can based on the documentation and examples I've found, but I'm getting errors I don't understand or scripts that just fail to do anything (I hope).

My task is, I think, fairly simple. I'm trying to provide a script I've found that performs most of the work I need to do with appropriate arguments, all of which are paths or file names:

on open theFiles
    -- Assume dropped files are are in the same folder
    -- Call SaveFilesAsSVG to create svg versions of each dropped file next to the originals in the same folder
end open

on run
    -- Ask for a destination folder with a defined relative path as the default
    -- Ask for a source folder with a defined relative path as a default
    -- Call SaveFilesAsSVG to create an svg version of each file with a defined extension found in the source folder in the destination folder
end run

The challenge for me is generating arguments in the form that the script I've found (p. 182) expects:

-- From Adobe:
on SaveFilesAsSVG(fileList, filePath, destFolder)
    set destPath to destFolder as string
    set fileCount to count of fileList
    if fileCount > 0 then
        repeat with i from 1 to fileCount
            set fileName to item i of fileList
            set fullPath to filePath & fileName
            set newFilePath to destPath & fileName & ".svg"
            tell application "Adobe Illustrator"
                open POSIX file fullPath as alias without dialogs
                export current document to file newFilePath as SVG ¬
                    with options {class:SVG export options ¬
                    , embed raster images:true}
                close current document saving no
            end tell
        end repeat
    end if
end SaveFilesAsSVG

Any help in writing my open and run handlers as specced would be greatly appreciated!


Solution

  • You can try something like this:

        on open droppedItem
        -- Assume dropped files are are in the same folder
        -- Call SaveFilesAsSVG to create svg versions of each dropped file next to the originals in the same folder
        set droppedItem to first item of droppedItem
        tell application "System Events" to kind of droppedItem = "Folder"
        if the result then
            tell application "System Events" to set myFileList to files of droppedItem whose visible is true
            SaveFilesAsSVG(myFileList, droppedItem's POSIX path, droppedItem)
        end if
    end open
    
    
    on run
        -- Ask for a destination folder with a defined relative path as the default
        set mydestFolder to (choose folder with prompt "Select destination folder")
    
        -- Ask for a source folder with a defined relative path as a default
        set myFilePath to (choose folder with prompt "Select source folder")
    
        -- Create fileList
        tell application "System Events" to set myFileList to files of myFilePath whose visible is true
    
        -- Call SaveFilesAsSVG to create an svg version of each file with a defined extension found in the source folder in the destination folder
        SaveFilesAsSVG(myFileList, myFilePath's POSIX path, mydestFolder)
    end run
    
    
    on SaveFilesAsSVG(fileList, filePath, destFolder)
        set destPath to destFolder as string
        set fileCount to count of fileList
        if fileCount > 0 then
            repeat with i from 1 to fileCount
                tell application "System Events" to set fileName to (item i of fileList)'s name
                set fileNameBase to getBaseName(item i of fileList)
    
                set fullPath to filePath & fileName
                set newFilePath to destPath & fileNameBase & ".svg"
                tell application "Adobe Illustrator"
                    open POSIX file fullPath as alias without dialogs
                    export current document to file newFilePath as SVG ¬
                        with options {class:SVG export options ¬
                        , embed raster images:true}
                    close current document saving no
                end tell
            end repeat
        end if
    end SaveFilesAsSVG
    
    
    on getBaseName(myFile)
        tell application "System Events" to set {fileName, fileExt} to {name, name extension} of myFile
        return text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
    end getBaseName