You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

backslash in applescript variable

I've a windows file path which uses backslashes and I want to add to a variable but applescript will not treat the slash as a text character. I want to pass to the variable from the clipboard and replace with the forward slashes so I can use the go to folder in finder. The windows file path is not always the same, so I can't manually edit to a double backslash. thanks

Posted on Jul 5, 2011 4:17 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 5, 2011 4:25 PM

Try using:


set the_string to the clipboard

set new_string to ""

repeat with this_char from 1 to (count the_string)

if item this_char of the_string is not "\\" then

set new_string to new_string & item this_char of the_string

else

set new_string to new_string & "/"

end if

end repeat


(59194)

8 replies
Question marked as Top-ranking reply

Jul 5, 2011 4:25 PM in response to John H

Try using:


set the_string to the clipboard

set new_string to ""

repeat with this_char from 1 to (count the_string)

if item this_char of the_string is not "\\" then

set new_string to new_string & item this_char of the_string

else

set new_string to new_string & "/"

end if

end repeat


(59194)

Jul 5, 2011 4:45 PM in response to John H

Applescript strings use the backslash as an escape character (mostly for quotes, so that you can have a command like set bob to "he said\"Wow!\"" which includes the quote character. In most cases applescript will quote this for you, for instance if you copy a backslashed path onto the clipboard and use a command like set txt to the clipboard, applescript will get a properly escaped version of the string from the clipboard. about the only time you will ever see this backslash problem is when you manually type a backslash into an applescript string. just type \\ anywhere you would normally type \. or instance, to pass a window's string to the clipboard, use something like the following


set the clipboard to "C:\\user\\docs\\Letter.txt"


It looks a little odd, but you get used to it. 😝

Nov 6, 2014 6:25 AM in response to Niel

How would you do this with the following applescript code (which causes a syntax error: selection of {application "Finder", "\""} can't be transformed into text type):


tell application "Finder" & quote

set sel to the selection as text


set the clipboard toPOSIX path of sel

end tell

end tell

My goal is to create a automator workflow using applescript which copies the path of a finder object to the clipboard. In Yosemite.

backslash in applescript variable

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.