Setting Icon on Folder of File from AS

Hi all here @ MacScripter!

First post, but there’ll be lots more coming :wink:

I’m currently completely stuck (after 6 hours of poking around in AS and Cocoa) trying to set a custom icon on just a simple file.
I gathered from searching here and google that AS doesn’t have the tools up for it, so that a call the a cocoa method is necessary. No problemo with me, really.
But for the life of me, I can’t get the friggin thing to work.
Any help is appreciated!


set IconFile to ((path to desktop) as string) & "icontemplate.icns"
set theImage to load image (POSIX path of IconFile)
set theFile to ((Home as string) & "test")
call method "setIcon:forFile:options:" with parameters {IconFile, theFile, 0}
delete theImage

It runs, no errors, but no changes to theFile either.
The call method should return a BOOL containing true or false, but if I set it to a variable, the variable doesn’t get filled.

I’ve tried other ways to the call method like :

call method "setIcon:forFile:options:" with parameters {IconFile, theImage, 0}

but the first call method above was the one I ended up with before breaking and going out to go ask for help from the pros :slight_smile:

Hope someone can help me with this.
Thanks in advance!

Welcome. :slight_smile:

Did you happen to try it like this?

call method "setIcon:forFile:options:" with parameters {theImage, IconFile, 0}

That’s all I have offhand.

Also: setIcon:forFile:options:

Thanks :cool:

No I didn’t, but it’s not what I want.
I want to set the icon for the file “theFile” to the icon which is supplied in IconFile.
I thought [NSWorkspace setIcon:(NSImage *) forFile:(NSString *)] really meant that the setIcon call needed an image.
So I tried to supply an image by loading the image in IconFile (Icons are images, right) into theImage as an AS object and feeding it into the call method.
Alas…

Could I get an example value for Home?

oops, sorry about that. Thought I supplied all the vars :

property Home : (path to home folder) as alias

I don’t it the cause of your problem, but you probably need to use a POSIX path:

set theFile to POSIX path of ((Home as Unicode text) & "test")

OK, sounds good, done it but indeed, it doesn’t help the cause :slight_smile:
Still not getting any changes to “theFile”.

What might be good thing to investigate on a sidetrack, is the lack of return value from the call method.
I use

set retVal to call method "setIcon:forFile:options" with parameters {theImage, theFile, 0}
try
    log retVal
end try

and it returns complete and utter nothingness. When I don’t “try” the app just finds itself in limbo…
Docs state that it should return a boolean value, instead it gives nothing? So maybe the call method doesn’t get called at all?

set theFilePath to "/Users/jed/Desktop/someFile.txt"
set theIcon to (load image "/Users/jed/Desktop/someIcon.icns")
set sharedWorkspace to call method "sharedWorkspace" of class "NSWorkspace" --> kinda important
set didSetIcon to call method "setIcon:forFile:options:" of sharedWorkspace with parameters {theIcon, theFilePath, 0}

Yeah, kinda. :stuck_out_tongue: I figured it was something like that, but I hadn’t gone back to testing it yet.

so that’s how it’s done :slight_smile: I was struggling with this, knowing it was needed in Cocoa and not being able to find anything about this kind of stuff in the docs.
Tell me, jobu, how did you know about this and where did you find it?

Thanks all!

Sorry, I realise that I am swimming with far bigger AS fish in this topic, but when I try to compile the above script in Script Editor I get a syntax error (with “image” in line two highlighted): Expected “,” but found identifier. Any idea what I might be doing wrong?

And on a related note, I have come to this topic because I have had no luck with “set icon of file thisFile to icon of file thatFile” from this topic: http://bbs.macscripter.net/viewtopic.php?pid=1144. Am I right in thinking that this sort of straight icon transference is not available via AS in 10.5?

Many thanks

Model: G5 2.5D
AppleScript: 2.0.1
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

The code above is AppleScript Studio terminology, which Script Editor is not able to compile.