Thursday, December 5, 2019

#1 2019-11-21 04:08:25 pm

dwghtjhnsn
Member
Registered: 2019-11-21
Posts: 3

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


Filed under: disk icon, alias target

Offline

 

#2 2019-11-22 01:53:53 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 756

Re: Needing help with scripting alias target and disk icon changes

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

2) "Change the name to JOHNSON-TW"

Applescript:

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"

3) "Copy johnsonTaiwan.icns file to the memory stick"

Applescript:

set anIconFile to choose file
tell application "Finder" to duplicate anIconFile to USBDisk

4) "Change the disk icon to that icns file"

Applescript:

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

5) "Copy all files from a folder"

Applescript:

set sourceFolder to choose folder
tell application "Finder" to duplicate files of sourceFolder to USBDisk

6) "Change the target for a copied Alais file that points to index.html in root of the memory stick"
??? can't understand

7) "Eject memory stick"

Applescript:

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

Last edited by KniazidisR (2019-11-22 03:00:37 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-11-22 08:23:33 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 234

Re: Needing help with scripting alias target and disk icon changes

KniazidisR wrote:

6) "Change the target for a copied Alais file that points to index.html in root of the memory stick"
??? can't understand


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

Applescript:


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

Last edited by peavine (2019-11-22 09:06:57 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2019-11-22 11:32:06 am

dwghtjhnsn
Member
Registered: 2019-11-21
Posts: 3

Re: Needing help with scripting alias target and disk icon changes

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

Dwight

Offline

 

#5 2019-11-22 09:53:33 pm

dwghtjhnsn
Member
Registered: 2019-11-21
Posts: 3

Re: Needing help with scripting alias target and disk icon changes

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.)

Offline

 

#6 2019-11-23 03:49:48 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 756

Re: Needing help with scripting alias target and disk icon changes

dwghtjhnsn wrote:


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


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

dwghtjhnsn wrote:


* 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.


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.

Last edited by KniazidisR (2019-11-23 04:04:41 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#7 2019-11-23 05:42:14 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 756

Re: Needing help with scripting alias target and disk icon changes

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


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#8 2019-11-23 09:34:12 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11625
Website

Re: Needing help with scripting alias target and disk icon changes

KniazidisR wrote:


Applescript:

set USBDisk to (((path to startup disk) as string) & ":Volumes:NO NAME") as alias



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

Applescript:

set USBDisk to "NO NAME" as alias

and to get the POSIX path

Applescript:

set USBDiskPath to POSIX path of "NO NAME"

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

Applescript:

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

Apart from that the coercion

Applescript:

((path to startup disk) as string)

is not wrong but less efficient than the as parameter

Applescript:

(path to startup disk as string)

Last edited by StefanK (2019-11-23 09:38:10 am)


regards

Stefan

Offline

 

#9 2019-11-23 10:06:26 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 234

Re: Needing help with scripting alias target and disk icon changes

KniazidisR wrote:

Applescript:

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


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:

Applescript:

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.

Last edited by peavine (2019-11-23 02:54:52 pm)


2018 Mac mini - macOS Catalina

Offline

 

#10 2019-11-23 03:57:20 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 756

Re: Needing help with scripting alias target and disk icon changes

peavine wrote:


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


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.

Last edited by KniazidisR (2019-11-23 04:02:25 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#11 2019-11-23 05:02:41 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 234

Re: Needing help with scripting alias target and disk icon changes

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?


2018 Mac mini - macOS Catalina

Offline

 

#12 2019-11-23 09:28:43 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6062

Re: Needing help with scripting alias target and disk icon changes

peavine wrote:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#13 2019-11-24 08:20:37 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 234

Re: Needing help with scripting alias target and disk icon changes

Shane Stanley wrote:
peavine wrote:

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.


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)