Hudson Serge
Hudson Serge

Reputation: 155

Applescript setting desktop picture to file in current directory

I'm attempting to set the current desktop picture to an image in the current directory of the script. For example, if my folder structure is:

~/Documents/Scripts/DesktopImageScript/image.jpg

With the script in the same directory, I want to be able to set the desktop image to the image.jpg without directly referring to the folder structure. The code I am currently using to fetch the current directory is:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me))

The issue isn't in that code as I can run the following command with the expected and correct results:

do shell script "echo " & app_directory

I believe the issue is in the code I'm using to set the desktop image:

tell application "Finder" set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg")) end tell

The error I receive when I try to run the script is:

error "Finder got an error: AppleEvent handler failed." number -10000

Not really sure what could be causing the error or how to fix it. Any help is appreciated. The full script is below:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me)) tell application "Finder" set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg")) end tell

Upvotes: 1

Views: 691

Answers (1)

Hudson Serge
Hudson Serge

Reputation: 155

Solved by using code from another answer on StackOverflow.

tell application "System Events"
    set theDesktops to a reference to every desktop
    repeat with x from 1 to (count theDesktops)
        set picture of item x of the theDesktops to app_directory & "/image.jpg"
    end repeat
end tell

Upvotes: 1

Related Questions