【问题标题】:Change icon of folder with AppleScript?使用 AppleScript 更改文件夹图标?
【发布时间】:2013-11-24 09:20:47
【问题描述】:

我创建了一个对我非常有帮助的 AppleScript,我希望是否可以自动更改文件夹图标。

这个脚本很简单,创建一个文件夹,然后在同一个文件夹中创建一个空文本文件。

这是脚本:

tell application "Finder"
    set newfolder to make new folder with properties {name:My Folder}
    make new file at newfolder with properties {name:"read_me.txt"}
end tell

是否可以自动更改文件夹图标?

(当然,我的自定义文件夹图标 (.icns) 与脚本位于同一文件夹中)

【问题讨论】:

    标签: applescript


    【解决方案1】:

    这是一个使用此软件包中的命令行实用程序“seticon”的解决方案:https://github.com/vasi/osxutils

    假设您的脚本、icns 文件和新文件夹都在同一个目录中。

    tell application "Finder"
        set parent_path to ((the container of the (path to me)) as text)
        set target_folder_path to quoted form of POSIX path of (parent_path & "my folder")
        set icon_path to quoted form of POSIX path of (parent_path & "icon.icns")
        do shell script "/usr/local/bin/seticon -d " & icon_path & " " & target_folder_path
    end tell
    

    【讨论】:

    • 感谢您的帮助!为此,我必须使用脚本自动将 .icns 文件复制并粘贴到父文件夹中,这可能吗?
    • 当然没有问题,或者你可以将你的 .icns 保存在一个固定的位置并使用绝对路径。 tell 中的前 3 行只是处理路径,do shell 脚本是关键部分,您可以将所需的任何路径传递给它。
    • 非常感谢! (查看我的个人资料,关于 Applescript 的另一个问题 ;-))
    • 项目链接已损坏。这似乎是同一个:github.com/vasi/osxutils
    【解决方案2】:

    我在 2021 年的解决方案,使用 SF-Symbols

    -- Help
    -- folder_path  : The folder whose icons will be changed (multiple selection in Finder possible)
    
    -- icon_path    : The Icon path
    -- archiv_path  : The Icon path for a folder named "Archiv"
    
    -- Presets here as used by me
    property folder_path : "/Users/ronny/Desktop/osxutils-master"
    property icon_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Folder.icns"
    property archiv_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Archiv.icns"
    
    -- Frameworks and Additions
    use framework "Foundation"
    use framework "AppKit"
    use scripting additions
    
    -- Create list (array) with selected items
    -- Be carefull, every icon will be changed
    
    tell application "Finder"
        set theListe to selection as list
    end tell
    
    repeat with i in theListe
    
        set aName to name of i
        log (aName)
    
        set destPath to POSIX path of (i as text)
    
        if aName is equal to "Archiv" then
            set sourcePath to archiv_path
        else
            set sourcePath to icon_path
        end if
    
        set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:sourcePath)
        (current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)
    
    end repeat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      相关资源
      最近更新 更多