Tuesday, September 29, 2020

#1 2002-12-12 03:35:29 am

Eelco Houwink
Member
Registered: 2002-11-20
Posts: 317

Duplicate custom icons between folders

I want to copy a custom icon from one to another folder and made this script.
However, Finder claims that file1 (invisable icon file in the first folder) cannot be found.  File ".DS_Store",also invisable, however CAN be found.

What am I doing wrong..?

tell application "Finder"
set aFold to choose folder "Select folder to copy icon"
set bFold to choose folder "Select folder to transfer to"
set aFold to aFold as string
set file1 to (aFold as string) & ".DS_Store"
set file2 to (aFold as string) & "Icon"
copy alias file1 to bFold
copy alias file2 to bFold
end tell


_________________
Eelco Houwink


Eelco Houwink


Filed under: Finder

Offline

 

#2 2019-07-31 09:06:49 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4569

Re: Duplicate custom icons between folders

As you ask to copy the custom icon applied to a given folder to an  other folder you may use :

Applescript:

# Use a CLI available from : [url]http://www.bluem.net/en/mac/cliclick/[/url]

# Edit these three instructions to fit your needs

set p2d to path to desktop as text
set theSource to (p2d & "source:") as alias
set theTarget to (p2d & "target:") as alias

tell application "Finder"
   select theSource
   tell me to set the clipboard to "" # an old habit
   tell application "System Events" to tell process "Finder"
       set frontmost to true
       keystroke "i" using {command down} # open infos window
       set infoWindow to window 1 --> "Infos sur source"
       tell infoWindow
           --class of UI elements -->{scroll area, button, button, button, image, static text}
           tell scroll area 1
               class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, checkbox, checkbox, UI element, static text, static text, static text, UI element, static text, text field, checkbox, UI element, static text, scroll area, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox, scroll bar}
               
               set {xLeft, yTop} to position of image 1
               set {w, h} to its size of image 1
               tell me to do shell script "/Users/admin/bin/cliclick c:" & xLeft + w div 2 & "," & yTop + h div 2
               delay 0.1
               keystroke "c" using {command down}
           end tell
       end tell
   end tell
   
   select theTarget
   
   tell application "System Events" to tell process "Finder"
       set frontmost to true
       keystroke "i" using {command down} # open infos window
       set infoWindow to window 1 --> "Infos sur target"
       tell infoWindow
           --class of UI elements -->{scroll area, button, button, button, image, static text}
           tell scroll area 1
               class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, checkbox, checkbox, UI element, static text, static text, static text, UI element, static text, text field, checkbox, UI element, static text, scroll area, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox, scroll bar}
               
               set {xLeft, yTop} to position of image 1
               set {w, h} to its size of image 1
               tell me to do shell script "/Users/admin/bin/cliclick c:" & xLeft + w div 2 & "," & yTop + h div 2
               delay 0.1
               keystroke "v" using {command down}
           end tell
       end tell
   end tell
end tell # Finder

CAUTION: the custom icon will disappear if you apply the command update to the target folder.

Just for info, the name of the custom icon file is not "Icon" but it's "Icon" & return

I tried to run:

Applescript:

tell application "Finder"
   set aFold to choose folder "Select folder to copy icon"
   set bFold to choose folder "Select folder to transfer to"
   set aFold to aFold as string
   set file1 to aFold & ".DS_Store"
   set file2 to aFold & "Icon" & return
   try
       duplicate alias file1 to bFold # Here ".DS_Store" is not available
   end try
   duplicate alias file2 to bFold
end tell

The file whose name is "Icon" & return is correctly duplicated but the Finder doesn't use it as icon.


Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 31 juillet 2019  16:57:57

Last edited by Yvan Koenig (2019-07-31 09:37:16 am)

Offline

 

#3 2019-07-31 09:14:31 pm

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

Re: Duplicate custom icons between folders

Eelco Houwink wrote:

I want to copy a custom icon from one to another folder



Here's an AppleScriptObjC solution:

Applescript:

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

set aFold to POSIX path of (choose folder "Select folder to copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to ws's iconForFile:aFold
ws's setIcon:theImage forFile:bFold options:0

There's one potential down-side: the new image will be a tiny bit lighter than the original on a retina screen. The difference is pretty minor, but it accumulates if you repeatedly copy from one folder another.


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

Offline

 

#4 2019-07-31 10:31:41 pm

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

Re: Duplicate custom icons between folders

Thank you, Shane, for nice solution. Goes to the library.

Last edited by KniazidisR (2019-07-31 10:40:46 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2019-08-01 03:04:31 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4569

Re: Duplicate custom icons between folders

Thank you Shane.

Is it a way to achieve the same when the source is a file ?

I tried to use:

Applescript:

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

set aFile to POSIX path of (choose file "Select file to copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to ws's iconForFile:aFile
ws's setIcon:theImage forFile:bFold options:0

but I get the generic icon because the Finder is set to display the pictures as icon.
May we use the content of the file to do the job?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 1 août 2019  11:04:25

Offline

 

#6 2019-08-01 06:14:44 am

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

Re: Duplicate custom icons between folders

Yvan Koenig wrote:

Is it a way to achieve the same when the source is a file ?



Sure:

Applescript:

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

set aFile to POSIX path of (choose file "Select file to copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:aFile
ws's setIcon:theImage forFile:bFold options:0


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

Offline

 

#7 2019-08-01 08:31:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4569

Re: Duplicate custom icons between folders

Thank you Shane.

I was afraid that the datas returned by

set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:aFile

was not in the correct structure.

I assume that the icon installed this way will not resist to tell application "Finder" to update bfold.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 1 aout 2019  16:28:50

Offline

 

#8 2019-08-01 05:34:31 pm

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

Re: Duplicate custom icons between folders

Yvan Koenig wrote:

I assume that the icon installed this way will not resist to tell application "Finder" to update bfold.



That's right. I don't see any way around that Finder bug (other than everyone logging bugs and someone at Apple fixing it).


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)