【问题标题】:How to specify collections of files and folders for processing with Applescript?如何指定要使用 Applescript 处理的文件和文件夹的集合?
【发布时间】:2026-01-18 06:30:02
【问题描述】:

我对 AppleScript 不是很熟练,并且对命名和枚举文件和路径的各种方法感到完全困惑。根据我找到的文档和示例,我已经尝试了几乎所有我能做的事情,但是我遇到了我不理解的错误或无法执行任何操作的脚本(我希望如此)。

我认为我的任务相当简单。我正在尝试提供一个我发现的脚本,它可以使用适当的参数执行我需要做的大部分工作,所有这些都是路径或文件名:

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

我面临的挑战是以script I've found (p. 182) 期望的形式生成参数:

-- 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

在编写我的openrun 处理程序方面的任何帮助将不胜感激!

【问题讨论】:

    标签: path applescript filenames adobe-illustrator


    【解决方案1】:

    你可以试试这样的:

        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
    

    【讨论】:

    • 谢谢!这是一个很大的帮助,但我得到“无法获取“.DS_Store”的文本 1 到 0。跌落和奔跑。
    • 另外:我希望能够删除多个文件(而不是文件夹),并在 run 情况下过滤文件扩展名。
    最近更新 更多