Escaped path in AppleScript adds leading / 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\"") &amp; "/" &amp; (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\" " &amp; 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\"") &amp; "/" &amp; (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") &amp; text 2 thru -1 of currentWallpaperPath

    do shell script "open -b \"com.adobe.Photoshop\" " &amp; 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.
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") &amp; 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