My objective is to create a script that sets my company logo as the icon of a webpage-shortcut-file on user Desktop.

The background is IT-management. I want to run a remote script on 100+ Macs to place a branded shortcut to my support portal on everybody's Desktop.

I found a piece of AppleScript that should change the file icon. The problem is that it works fine when I test it in Script Editor, but if I do it in Terminal, it runs, but doesn't complete the task.

Here's the code (h/t How can I change a file or folder icon using the Terminal):

#!/usr/bin/env osascript

use framework "AppKit"

property this : a reference to current application
property NSWorkspace : a reference to NSWorkspace of this
property NSImage : a reference to NSImage of this

on run
    set icon to "Users/myuser/Downloads/supporticon.png"
    set target to "Users/myuser/Desktop/supportpage.webloc"
    setIcon to icon for target
end run
to setIcon to iconPath for filePath
    set sharedWorkspace to NSWorkspace's sharedWorkspace()
    set newImage to NSImage's alloc()
    set icon to newImage's initWithContentsOfFile:iconPath
    set success to sharedWorkspace's setIcon:icon forFile:filePath options:0
end setIcon

Running it in Script Editor gives result: true (and the icon actually changes).

Running it in Terminal: My-MBP:~ myuser$ osascript setIcon2.scpt gives false and nothing happens. But I need for it to work in Terminal, because that's how the remote management software runs the script.

As my proficiency level in AppleScript is "monkey pushing buttons", the solution is probably something blindingly obvious. But could anyone help me to figure it out, please?

  • 2
    Is it really "set icon to "Users/" why have you left the / off? ir should be "set icon to "/Users/" – mmmmmm Feb 9 at 12:21
  • 1. The #!/usr/bin/env osascript shebang is nothing more than a comment when the script is saved as a .scpt file. - 2. If you add use scripting additions at the top of the script you can then use e.g. set icon to POSIX path of (path to downloads folder) & "supporticon.png" and set target to POSIX path of (path to desktop folder) & "supportpage.webloc" to avoid hardcoding e.g. /Users/$USER/.. - 3. In the last few releases of macOS, Terminal must be given Full Disk Access to e.g. Desktop and Downloads to run osascript setIcon2.scpt in this use case. – user3439894 Feb 9 at 14:42
  • BTW That AppleScript script was intended to be saved as a plain text file and made executable with chmod so as to be run as a stand-a-lone executable, hence the presence of the #!/usr/bin/env osascript shebang, which I would use #!/usr/bin/osascript instead as stand-a-lone shell script executable, – user3439894 Feb 9 at 15:18
  • The problem was indeed using "User/..." instead of "/User/..." Somehow it works fine in Script Editor, but not anywhere else, so thank you! I also figured out that the remote management software only works if I feed it plaintext AppleScript (not .scpt files) so the comment about .scpt not needing a shebang etc. helped too. The problem I'm left with is that scripting additions (universal paths) don't seem to work when deploying scripts via the remote management software. Could anyone recommend any other way to avoid hardcoding usernames for 100+ Macs? :S – Mart Roben Feb 10 at 15:27

You must log in to answer this question.

Browse other questions tagged .