-1

Have been Googling for hours with absolutely no luck. Decided to post here as a last resort. I'm trying to access setIcon:forFile:options: of NSWorkspace's sharedWorkspace. Take for example, the following code snippet:

set r to current application's NSWorkspace's sharedWorkspace's "setIcon:forFile:options:"

Upon execution of this snippet, the following error is thrown: Can't get current application's NSWorkspace's sharedWorkspace's "setIcon:forFile:options:". Access not allowed. (-1723). I have tried a number of different ways to circumvent this strange exception, but none have worked thus far. Any suggestions on how to proceed here would be very much appreciated.

Thanks.

broment
  • 76
  • 6
  • Nothing strange about the error message. Have you actually looked at some AppleScript-Objc to see what commands look like? For instance https://stackoverflow.com/a/59097037/341994 – matt Feb 02 '20 at 03:58
  • Thanks for the resource. Using that syntax, the exception thrown is: `[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key setIcon. (-10000)` – broment Feb 02 '20 at 04:08
  • Another example that sets the icon for an alias is [here](https://stackoverflow.com/a/54900620/10853463). It might help to include the code that you are using, since the errors in your later comments are not from statements in your original post. – red_menace Feb 02 '20 at 04:42

1 Answers1

0

The (single) literal string parameter makes no sense. The method has three parameters. Each colon represents one parameter. You have to specify an NSImage instance, a string file path and an integer value as options

set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:"/fullpath/to/image.icns"
set success to current application's NSWorkspace's sharedWorkspace's setIcon:theImage  forFile:"/fullpath/to/file.ext" options:0
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks. My original intention was to add a `with parameters {theImage, theFile, 0}` statement following the method call, but I was never able to get that far. Upon compiling the new code, I'm getting `-[NSURL length]: unrecognized selector sent to instance 0x7fc6d8c5ff00`. Not really sure what to make of that. – broment Feb 02 '20 at 04:34
  • `with parameters` is dead AppleScript Studio syntax. My suggested code doesn't affect `NSURL`. Both paths must be full valid string paths. – vadian Feb 02 '20 at 04:41