Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have the following AppleScript that I am trying to debug:

tell application "System Events"

    set theResult to (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value
FROM data WHERE data.ROWID = 2\"") & "/" & (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value
FROM preferences JOIN data on preferences.key = 16 AND preferences.picture_id = 1 AND preferences.data_id=data.ROWID\"")

    do shell script "open -b \"com.adobe.Photoshop\" " & quoted form of theResult

end tell

Basically what this script does is it fetches the path of the currently displayed wallpaper on my computer and passes that as an argument to Photoshop.

The path that is fetched is in the form ~/PATH/TO/IMAGE. This path has spaces in it which is why I used the quoted form of command to escape the string. However, in doing so the path to the image changes to /~/PATH/TO/IMAGE and I'm very puzzled as to why it adds the extra "/" to the beginning of the string. Because of this, the script does not run as expected. How can I escape the path but keep it from adding the extra "/". Is there a way to convert the path to a fully qualified path or a different way of escaping the string?

Updated version of script with correction:

tell application "System Events"

    set currentWallpaperPath to (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value
FROM data WHERE data.ROWID = 2\"") & "/" & (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value
FROM preferences JOIN data on preferences.key = 16 AND preferences.picture_id = 1 AND preferences.data_id=data.ROWID\"")

    if currentWallpaperPath starts with "~/" then set currentWallpaperPath to (system attribute "HOME") & text 2 thru -1 of currentWallpaperPath

    do shell script "open -b \"com.adobe.Photoshop\" " & quoted form of currentWallpaperPath

end tell

Also wanted to add that this script is only relevant to OS X Mavericks (10.9) as the wallpaper information has now moved to the database file rather than being referenced in the com.apple.Desktop.plist file as it was in previous versions of OS X. Also, my computer is set to randomly display wallpaper from a specific folder on my machine. The script may need to be tweaked depending on your computers configuration.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

quoted form of also escapes the tilde. quoted form of "~/Pictures" returns '~/Pictures'. open interprets '~/Pictures/' as a relative path, and the default working directory for do shell script is /.

I couldn't get the sqlite commands to work, but try something like this:

path=$(sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db 'select data.value from data'|tail -n1);open -b com.adobe.Photoshop "${path/#~/$HOME}"

Or remove the tilde with AppleScript:

set p to "~/Pictures"
if p starts with "~/" then set p to (system attribute "HOME") & text 2 thru -1 of p

You can also get the desktop picture of the current space with System Events:

tell application "System Events" to picture of current desktop
share|improve this answer
    
Thank you for the suggestion, the line if p starts with "~/" then set p to (system attribute "HOME") & text 2 thru -1 of p is all I needed to get this to work! I simply added this line before I make the do shell script call. –  Sly Raskal Nov 28 '13 at 17:23

Here is another way to create the path:

set databasePath to (POSIX path of (path to library folder from user domain as text)) & "Application Support/Dock/desktoppicture.db"
set theResult to (do shell script "sqlite3 " & quoted form of databasePath & " \"SELECT data.value FROM …… ; \"")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.