Setting icon for an alias using applescript

foundation
mojave
finder

#1

I am creating an alias and setting an icon for that using the script below. The icon is being set for High Sierra but not on El Capitan.

I am totally new to Applescript, so no idea why this isn’t working for El Capitan.

use framework "Foundation"
use scripting additions
set ideContentsPath to ".../IDE/WebOSIDE.app/Contents" -- path of Contents folder which i replace at runtime from my java program
set ideExec to ideContentsPath & "/MacOS/WebOSIDE"
set sourceFile to (POSIX file ideExec)
tell application "Finder"
  set newAlias to (make new alias file at desktop to sourceFile) as alias
  set iconPath to ideContentsPath & "/Resources/WebOSIDE.icns" 
  my setIcon(newAlias,iconPath)
  set name of newAlias to "My Shortcut"
  duplicate newAlias to folder "my Dir" of folder "Applications" of startup disk
end tell
to setIcon(fileRef,iconPath)
  set iconImage to current application's NSImage's alloc's initWithContentsOfFile:iconPath
  current application's NSWorkspace's sharedWorkspace's setIcon:iconImage forFile:(POSIX path of fileRef) options:0
end setIcon

I need a solution which works across El Capitan to High Sierra.

Thanks


(Jonas Whale) #2

Try this.
You just need to replace the paths.

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"
use scripting additions

set sourceFolder to "/Applications/Safari.app/Contents"
set sourcePath to sourceFolder & "/MacOS/Safari"
set iconPath to sourceFolder & "/Resources/compass.icns"

set destPath to "/Applications/My Dir/My Shortcut"

set sourceURL to current application's NSURL's fileURLWithPath:sourcePath
set destURL to current application's NSURL's fileURLWithPath:destPath
set theOptions to ((current application's NSURLBookmarkCreationPreferFileIDResolution) as integer) + ((current application's NSURLBookmarkCreationSuitableForBookmarkFile) as integer)
set bookmarkData to sourceURL's bookmarkDataWithOptions:theOptions includingResourceValuesForKeys:{} relativeToURL:(missing value) |error|:(missing value)
set {theResult, theError} to current application's |NSURL|'s writeBookmarkData:bookmarkData toURL:destURL options:0 |error|:(reference)
if theResult = false then error (theError's localizedDescription()) as text

set iconImage to current application's NSImage's alloc()'s initWithContentsOfFile:iconPath
current application's NSWorkspace's sharedWorkspace()'s setIcon:iconImage forFile:destPath options:0

#3

Tried by creating a shortcut in /Applications/My Dir itself but I get the exec icon image on the shortcut , not the compass icon.


(Jonas Whale) #4

compass.icns is the name of the Safari icon.
You need to replace it with the name of your application icon.
According to your script, it should be WebOSIDE.icns


#5

I meant I tried with your paths itself pointing to safari , to first check if it creates the shortcut with icon. I was expecting the Safari compass icon in the shortcut but it didn’t show up.


(Jonas Whale) #6

What happens if you run the script, then duplicate the created alias with command+D in the Finder?
Or renaming it (with your hands on the keyboard)?


#7

Tried both, duplicating with command+D and rename but the icon image didn’t change.


(Jonas Whale) #8

Have you cleared the “My dir” folder of any file named “My shortcut” previously to run the script?


#9

“My dir” folder is empty.


(Jonas Whale) #10

That’s weird.
The last thing you can check is the name of your Safari icon.
After that, I’m stuck.

Maybe you can try with the paths to your application?