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 DECLARATIONS:
property this : a reference to current application
property NSWorkspace : a reference to NSWorkspace of this
property NSImage : a reference to NSImage of this
--------------------------------------------------------------------------------
# IMPLEMENTATION:
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
--------------------------------------------------------------------------------
# HANDLERS:
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?
#!/usr/bin/env osascript
shebang is nothing more than a comment when the script is saved as a .scpt file. - 2. If you adduse 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"
andset 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 runosascript setIcon2.scpt
in this use case.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,