Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Copy/paste icons & icns using AppleScriptObjC

I'm opening a new thread following this one to see what can be done with AppleScriptObjC.

As an Applescripter since 1992, it's my duty to learn this language.


My need is very simple on paper: copy an icon, icns or image file to paste it on a Finder item.


Running Mountain Lion 10.8.5, I'm still using Xcode 3.2.6 because I'm not familiar with AppleScriptObjC and I need to make and update several Applescript based applications for my work.

If I'm not wrong, it's impossible to have 2 versions of Xcode on the same startup disk.


Is it possible to write AppleScriptObjC scripts in Applescript Editor or even Script Debugger?


Red_menace, if you are here…


😉

iPhone 4, OS X Mountain Lion (10.8.5)

Posted on Oct 15, 2013 12:26 AM

Reply
12 replies

Oct 15, 2013 5:58 AM in response to ionah

Yes, you can write AppleScriptObjC in the Script Editor. It's a little odd to start: you have to create a new script by using File->New from Template->Cocoa-AppleScript Applet, and you have to use Run Application rather than just Run, but other than that it's business as usual.


My ASObjC is a little rusty, but you'll want to use NSWorkspace's setIcon:ForFile:options: method. Give me a moment and I'll see if I can get it to work.

Oct 15, 2013 6:20 AM in response to ionah

ok, this seems to work. Create an ASObjC file as discussed above and copy in the following code:


property NSWorkspace : class "NSWorkspace"

property NSImage : class "NSImage"


set filePath to "/path/to/file to iconize"

set imagePath to "/Path/to/image file for icon"


set image to NSImage's alloc's initWithContentsOfFile_(imagePath)

NSWorkspace's sharedWorkspace()'s setIcon_forFile_options_(image, filePath, 0)

The first two and last two lines are the ObjC bits, the rest is standard applescript. Adjust as needed.

Oct 15, 2013 7:11 AM in response to ionah

I have a Set Icon Cocoa-AppleScript application that I've been tweaking off and on over the years - it can be downloaded here. The project isn't very big, but uses NSWorkspace methods to set, copy, and clear icons, as well as a couple of other things such as a progress bar class.


As mentioned earlier, AppleScriptObjC in the AppleScript Editor is a little bit different in that you can't run it directly from the editor, and there are also some differences in the way a Cocoa application works, but for smaller projects it works OK. There are a couple of third party editors out there such as the AppleScriptObjC Explorer, and of course there is Xcode.

Oct 15, 2013 9:35 AM in response to red_menace

@Red_menace

Your project is a piece of art!

But I need time to read and undersatand everything.


It's not so hard. Just some things are obscure for me.

For example, I understand why you call current application's to get NSEvent's modifierFlags()

But why for NSImage's alloc's initWithContentsOfFile_??


@twtwtw

I made a Applet as you said and pasted in the following code.

No way to make it run.

I'm missing something. But what?



script iconTest


property NSWorkspace : class "NSWorkspace"

property NSImage : class "NSImage"


on run



-- first, an icns file

set filePath to "/Users/xxx/Desktop/iconChange/dossier sans titre 1"

set imagePath to "/Volumes/MacPro 2/xxx/Icones en cours/2013 icons by Factory/excel/excel-16.icns"


set image to NSImage's alloc's initWithContentsOfFile_(imagePath)


NSWorkspace's sharedWorkspace()'s setIcon_forFile_options_(image, filePath, 0)



-- second, a folder with custom icon

set filePath to "/Users/xxx/Desktop/iconChange/dossier sans titre 2"

set imagePath to "/Volumes/MacPro 2/xxx/Icones en cours/2013 icons by Factory/marques"


set image to NSWorkspace's sharedWorkspace's iconForFile_(imagePath)


NSWorkspace's sharedWorkspace's setIcon_forFile_options_(image, filePath, 0)


end run


end script

Oct 15, 2013 10:03 AM in response to ionah

If you liked that, you are really going to have fun with the ProgressBarController (in the application bundle) 😁.


In an AppleScriptObjC application, the various Cocoa classes and constants are defined in the runtime wrapper, so to use them you target the current application. When an object is created (such as an NSImage) it is both a class and an instance, so you no longer need to target the current application since the class is already defined.


If you are going to be using properties for shortcuts to the current application classes, their definitions need to target the current application or be at the application level outside of a script object, otherwise they will be out of scope. I started out using that kind of shortcut, but now just use one for current application, for example:


propertyCocoa : current application


then statements look like:


Cocoa's NSImage's alloc's initWithContentsOfFile_(whatever)


A little bit more self-commenting, but you still have to be careful when copy/pasting statements into a situation where the shortcut may not be defined, such as posting to a forum.

Oct 15, 2013 10:12 AM in response to ionah

ionah wrote:


@twtwtw

I made a Applet as you said and pasted in the following code.

No way to make it run.

I'm missing something. But what?


The script editor is kind of flaky with ASObjC apps. My guess is that when you run an app from the script editor, the script editor keeps an open reference to the app so that trying to run the app itself doesn't run the run handler. The solution is that when you're done testing, save the app somewhere convenient, quit the script editor entirely, and then run the applet.

Oct 15, 2013 11:38 AM in response to twtwtw

After a reboot and a little modification of the code, everything is OK.

property NSWorkspace : class "NSWorkspace"

property NSImage : class "NSImage"


on run


set filePath to "/Users/xxx/Desktop/iconChange/dossier sans titre 1"

set imagePath to "/Volumes/MacPro 2/xxx/Icones en cours/2013 icons by Factory/excel/excel-16.icns"


set image to NSImage's alloc's initWithContentsOfFile_(imagePath)


NSWorkspace's sharedWorkspace()'s setIcon_forFile_options_(image, filePath, 0)


set filePath to "/Users/xxx/Desktop/iconChange/dossier sans titre 2"

set imagePath to "/Volumes/MacPro 2/xxx/Icones en cours/2013 icons by Factory/marques"


set image to NSWorkspace's sharedWorkspace's iconForFile_(imagePath)


NSWorkspace's sharedWorkspace's setIcon_forFile_options_(image, filePath, 0)



quit


end run



Now, I would like to go further.

I've installed Mountain lion on a virtual machine of Parallels Desktop.

Will install the latest version of Xcode on it and create an AppleScriptObjC app.

A good way to learn (with the help of Red's example).


Then, maybe, create a service with AppleScriptObjC… (is it possible?)


In short, I'll be back soon!

😉

Copy/paste icons & icns using AppleScriptObjC

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.