5 Replies Latest reply: Jan 31, 2013 8:04 PM by red_menace
Futurefrog Level 1 Level 1 (0 points)

I just started trying to work with AppleScript and I have created a very simple app to mess with my friend that continually sets his background picture to something of my chosing. I am saving the AppleScript as an Application and am running OS X Mountian Lion.

 

My code looks like this.

 

repeat

          tell application "Finder"

                    set desktop picture to {"absolute/path/appname.app/Contents/Resources/picture.png"} as alias

          end tell

end repeat

 

The problem is that when ever I move the app to a different folder the path is not longer valid and the app ceases to work.  I have looked high and low online to find a solution to this but I am starting to think that it is so basic that no one bothers to point it out because everyone already knows...

 

If someone could please clue me in that would be incredibly helpful. Thanks a bunch!


MacBook Pro (15-inch, Mid 2012), OS X Mountain Lion (10.8.1)
  • 1. Re: Relative path to my app's Resource folder?
    twtwtw Level 5 Level 5 (4,690 points)

    Look in the Standard Additions osax for the path to resource command.  And note for future consideration that I've never seen a computer-based practical joke that the target thought was funny.  If you end up with a black eye, don't say you weren't warned.

  • 2. Re: Relative path to my app's Resource folder?
    Futurefrog Level 1 Level 1 (0 points)

    Ok so I found the Standard Additions Dictionary and revised my code with path to resource, however it keeps returning the error that the file cannot be found.

     

    I have tried both of the following code configurations:

     

    repeat

              tell application "Finder"

                        set desktop picture to path to resource in bundle "picture.png" as alias

              end tell

    end repeat

     

    and

     

    repeat

              tell application "Finder"

                        set desktop picture to path to resource "picture.png" as alias

              end tell

    end repeat

     

    How can I get this error to stop? The Standard Additions thing is not all that self explanatory. I have no idea how to actually insert what it is listing in to my code. The picture is in my resources folder and I can even see it when I open Bundle Contents inside of AppleScript.

  • 3. Re: Relative path to my app's Resource folder?
    Frank Caggiano Level 7 Level 7 (23,800 points)

    the resource is the file in the resouce folder of the app. So something like

     

    path to resource "file" in bundle (path to me)

     

    should work

  • 4. Re: Relative path to my app's Resource folder?
    twtwtw Level 5 Level 5 (4,690 points)

    Tha's because you're asking it to find the picture in the Finder's bundle.  do it this way (in principle):

     

    repeat

              set pic to path to resource "picture.png"

              tell application "Finder"

                        set desktop picture to pic

              end tell

    end repeat

     

    however, you don't want to do it that way (in practice) because it will give you an infinite loop that freezes up both the script and the Finder.  I assume you're saving this as an application, right?  Then make sure you save it as a stay open application, and use the following code:

     

    on idle

              set pic to path to resource "picture.png"

              tell application "System Events"

                        set picture rotation of every desktop to 0

                        set picture of every desktop to pic

              end tell

     

              return (random number from 15 to 45)

    end idle

     

    This will give you an app that runs continuously and changes the desktop picture every 15 to 45 seconds, and the idle handler will pu the application to sleep between changes so it doesn't eat up system resources.

  • 5. Re: Relative path to my app's Resource folder?
    red_menace Level 6 Level 6 (14,615 points)

    From the StandardAdditions dictionary:

     

    Parameters

    • string -- the name of the requested resource
    • [in bundle file] -- an alias or file reference to the bundle containing the resource (default is the target application or current script bundle)
    • [in directory string] -- the name of a subdirectory in the bundle’s “Resources” directory

    Result

    • alias -- the path to the resource

     

    The Contents/Resources/ folder in the current application bundle is the default directory, so all you need to use is the file name.  Also note that the Finder is not needed for this command, and in general you should avoid using any scripting addition command inside an application tell statement.