Is it possible for an AppleScript .scpt file to retain a custom icon, even after edits have been made to the code?
Generally speaking, when a User assigns a custom icon to a file by copying and pasting an image file into the icon, in the top left corner, on the file's Get Info sheet, the image file is stored as a com.apple.ResourceFork
extended attribute of the file it's applied to, and can be seen in Terminal using ls -l@ filename.ext
to show the file has extended attributes. To manipulate the extended attributes, use the xattr
utility in Terminal. Note that the use of the term image file in this contact is any valid source copied on the Clipboard to then be pasted in the Get Info sheet.
Having to reapply the icon file after having saved the .scpt file in Script Debugger causes the com.apple.ResourceFork
extended attribute to continue to grow in size, while possibly loosing access to the accumulated icon files, can't say for sure as I've not done enough testing. Unfortunately, while Script Editor keeps the custom icon file, nonetheless it appears to grow the size of the com.apple.ResourceFork
extended attribute as well.
To reduce the size of file by removing specifically the com.apple.ResourceFork
extended attributes, use the following command syntax in Terminal:
xattr -d com.apple.ResourceFork filename.ext
Then reapply the custom icon to the file.
Have a look at the man page for xattr
in Terminal by typing, xattr
and then right-click on it selecting Open man Page from the context-menu.
The issue with Script Debugger resetting to the default icon will probably have to be taken up with the Developer, or use the workaround suggested in wch1zpink's answer.
Since the com.apple.ResourceFork
extended attribute grows regardless of which of the apps you use, Script Editor or Script Debugger, this is an issue that Apple need to address.
I know you're using OS X El Capitan 10.11.6, however I tested this under macOS Sierra 10.12.5 and the behavior apparently being the same, it probably doesn't matter that I tested this in a later OS release. I was using Script Debugger 6.0.4.
Because of the repetitiveness of what you're doing, a scripted solution is in order here to reduce the effort necessary to maintain both a small file size, inclusive of it extended attributes, and the custom icon.
Update:
Having gone down a similar road, under OS X 10.8.5, with programmatically setting a file's icon, and having tested almost every solution presented under How can I change a file or folder icon using the Terminal, I chose to use setfileicon
from this answer, as it worded the best for my needs at the time.
The following AppleScript code is example code that can programmatically delete the com.apple.ResourceFork
extended attribute of the target file to reduce it accumulative size and reset the target file's icon. It is written around the use of the workaround suggested in wch1zpink's answer, referencing locking the file to keep the icon intact during a save under Script Debugger.
The main purpose of this example code is to show how to delete the com.apple.ResourceFork
extended attribute of the target file and reset its custom icon programmatically. I've thrown in the code to unlock/lock the target file if you choose to use that method of keeping the icon intact between saves, if not it can be removed. I guess it just depends on how often to want to reset the size of the target file's com.apple.ResourceFork
extended attribute.
tell current application
set theTargetFilename to "filename.scpt" as string
set theHomeFolderPath to (path to home folder) as string
set theScriptsFolder to (path to scripts folder) as string
set theTargetFilePathname to theScriptsFolder & theTargetFilename as alias
set theTargetIcon to quoted form of (POSIX path of (theHomeFolderPath & "bin:icon.icns") as string)
set theTargetExecutable to quoted form of (POSIX path of (theHomeFolderPath & "bin:setfileicon") as string)
tell application "Finder" to set locked of theTargetFilePathname to false
do shell script "xattr -d com.apple.ResourceFork " & quoted form of (POSIX path of theTargetFilePathname) & "; exit 0"
do shell script theTargetExecutable & " " & theTargetIcon & " " & quoted form of (POSIX path of theTargetFilePathname)
tell application "Finder" to set locked of theTargetFilePathname to true
end tell
Notes:
For the purpose of this example code, I've placed the setfileicon
command line utility and target icon.icns
files in the bin folder within the root of my Home folder. The icon.icns
file is the .icns file of the one linked in your question. You could use the PNG file, however I believe the .icns file produces a better image.
The script could be more hard coded and not as verbose, however I coded it this way for a few reasons of which I'm not going to get into the details of unless you need it explained. Feel free to utilize the example code to implement a working solution as needed/wanted in your environment.
This was tested this under macOS Sierra 10.12.5, however it should also work under OS X El Capitan 10.11.6.
Actually there is a solution. I downloaded the .icns file from the link you provided. I then dragged the entire .icns file from the Finder window to the icon image in the Get Info window of the script which I created in Script Debugger app. After the new icon was set, I selected the option to “lock file” in the Get Info window.
After this, I opened that script again in Script Debugger app and made a few changes to the script, recompiled the script and re-save it. When I tried to save it again, I got this message window.
I selected the option to “Save Ayway” and BINGO!! The edits were saved but the custom icon remained unchanged.
Here is a suggestion that maybe somebody can grab the ball and run with it. Instead of saving it as a script file in script debugger, save it as a script bundle (.scptd). After doing this, ctrl + click on that file in finder and select “Show Package Contents”. Add your custom .icns file to the Resources folder. You'll also notice an info.plist file. Open the info.plist file in Xcode. If you hit the + button on the information property list row, it gives the option to add icon files.
This is above the scope of my knowledge but maybe somehow the custom icons file can be set here.