Needing help with scripting alias target and disk icon changes

I need to create about 100 memory sticks as gifts. Each one will be set up to work on a Mac and PC.

Here are the steps I’m doing to create each one manually:
Mount memory stick (NO NAME)
Change the name to JOHNSON-TW
Copy johnsonTaiwan.icns file to the memory stick
Change the disk icon to that icns file
Copy all files from a folder
Change the target for a copied Alais file that points to index.html in root of the memory stick
Eject memory stick

I have seen examples here that I think I can use to do most of these tasks. The 2 things I can’t find any information on are automating changing the disk icon and changing the target of the alias file to the memory stick.

Thanks for any help you can offer

  1. “Mount memory stick (NO NAME)”
    No need, as USB devices are mounted by system automatically.

  2. “Change the name to JOHNSON-TW”

set USBDisk to (((path to startup disk) as string) & ":Volumes:NO NAME") as alias
tell application "Finder" to set name of USBDisk to "JOHNSON-TW"
  1. “Copy johnsonTaiwan.icns file to the memory stick”
set anIconFile to choose file
tell application "Finder" to duplicate anIconFile to USBDisk
  1. “Change the disk icon to that icns file”
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set USBDisk to (((path to startup disk) as string) & ":Volumes:JOHNSON-TW") as alias
set anIconFile to choose file

set anIconFilePath to POSIX path of anIconFile
set USBDiskPath to POSIX path of USBDisk

set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:anIconFilePath
ws's setIcon:theImage forFile:USBDiskPath options:0
  1. “Copy all files from a folder”
set sourceFolder to choose folder
tell application "Finder" to duplicate files of sourceFolder to USBDisk
  1. “Change the target for a copied Alais file that points to index.html in root of the memory stick”
    ??? can’t understand

  2. “Eject memory stick”

tell application "Finder" to eject disk "JOHNSON-TW"

I also do not fully understand this request but you can change the target of an existing alias file as follows:


set theAlias to choose file -- the existing alias file
set newAliasPath to choose file -- new target of existing alias file

tell application "Finder"
	set original item of alias file theAlias to file newAliasPath
end tell

SOURCE:
https://macscripter.net/viewtopic.php?id=34515

Thanks so much. This is great and I’ve learned a lot from both of these replies.

Dwight

KniazidisR:
Thanks to your help I’m able to do everything except for 2 things:

  • The folder contains subfolders. The script works if I just have files–with folders it seems to hang.

  • The script that changes the icon for the USBdisk creates a file called ‘icon?’ and then deletes it after a few seconds. The icon for the USBdisk doesn’t change.

(BTW I did muddle up what I was asking for about changing the target of an alias file. peavine figured out what I wanted and your solution works great. Thanks.)

Most likely, it doesn’t hang, but takes lot of time to duplicate contents of subfolders. Duplicate entire contents in Finder takes lot of time. And, USB sticks are slow too with writing to them

I don’t know why this script’s icon setting works fine with usual folders, and fails with folders of HardDisk/Volumes/ directory. Most likely, it is something with permissions… SIP…

Other is this : Disk icons should match. Look for icons that are square, ideally 512 by 512 pixels (or higher), and in Apple’s .icns format.

UPDATE: For icon setting:

Try this: set privileges of your USB stick for group “everyone” to READ & WRITE.

Then, the script above should make the job

This is wrong and cumbersome syntax (even if it works). The HFS path of a volume is represented just by the name of the volume. The folder Volumes is only used in POSIX paths to external volumes.

This is the recommended syntax to get the alias specifier of a volume

set USBDisk to "NO NAME" as alias

and to get the POSIX path

set USBDiskPath to POSIX path of "NO NAME"

To change the name of a disk in Finder use the disk specifier

tell application "Finder" to set name of disk "NO NAME" to  "JOHNSON-TW"

Apart from that the coercion

((path to startup disk) as string)

is not wrong but less efficient than the as parameter

(path to startup disk as string)

I’ve been looking for a script to change folder/file icons and KniazidisR’s script works great but with one issue.

If I manually change the icon for a folder/file in the “Info” panel (using the paste command), the size of the folder/file increases in an amount approximately equal to the size of the image file. In my test, a PNG file that contained 15 KB increased the size of the folder by 23 KB. After doing this, the folder contains a hidden file with the name “Icon?”, which contains 23 KB

I did the same thing using KniazidisR’s script and the folder increased in size by 234 KB. In this instance, the “Icon?” file in the folder shows 234 KB.

Is there some way to avoid this size increase? I tried both PNG and ICNS image files with the same result. I changed the script slightly to:

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

set theFile to choose file with prompt "Select the file."
set theIcon to choose file with prompt "Select the icon."

set iconPath to POSIX path of theIcon
set filePath to POSIX path of theFile

set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:iconPath
ws's setIcon:theImage forFile:filePath options:0

BTW, this post is somewhat off topic and if Nigel or the OP advises, I’ll create and move this to a new thread.

Thanks for the help.

This isn’t issue. The icons? on mac is created on every external disk (for custom icons). I think, for portability. This way, it knows what is its custom icon when you mount USB again. Removing icons? file is equivalent to restore to default icon.

KniazidisR. Thanks for the response. I didn’t explain properly, so let me give an example.

  1. I create a test AppleScript that only contains one line. I save the test AppleScript as an application on my boot drive and the script contains 100 KB.

  2. I add an icon to the test AppleScript utilizing my version of your script. The icon is a PNG file that contains 15 KB. Now, the test AppleScript contains 334 KB.

Why does adding a 15 KB icon to a 100 KB Applescript increase its size to 334 KB?

Because it creates versions at the various sizes and resolutions specified for .icns files. The extra size is a trade-off for having pre-scaled images to display.

Thanks Shane–that makes sense.