Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pierre1610

macrumors regular
Original poster
Feb 3, 2009
185
19
I'm trying to create a way of converting a windows path to a Mac path and back again. I've found varous examples of how to do this online but going from Mac to windows is causing me a problem. This seems to be mainly due to the script not being able to remove part of the folder structure to go to a mapped drive letter

Mac
Volumes/Storage/Dropbox/Live/1152_projectFolder/05_cg/renders/anim_test/RR01c_chalk_rl.0350.exr

Windows
S:\1152_projectFolder\05_cg\renders\anim_test\RR01c_chalk_rl.0350.exr

See the above examples, to go from Mac to Windows i need to remove "Volumes/Storage/Dropbox/Live/" from the path which i can't seem to do without causing an error.

Windows to Mac
on searchReplace(theText, SearchString, ReplaceString)
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's text item delimiters to OldDelims
return newText
end searchReplace

on run {input, parameters}
set myClip to the input
set mytext to searchReplace(myClip, "<", "")
set mytext to searchReplace(mytext, ">.", "")
set mytext to searchReplace(mytext, ">", "")
set findIt to "\\"
set replaceIt to "/"
set mylocation to searchReplace(mytext, findIt, replaceIt)
set mylocation to "smb:" & mylocation
set mylocation to searchReplace(mylocation, "smb:S:", "Volumes/Storage/Dropbox/Live/")
return mylocation
end run

Mac to Windows
on searchReplace(theText, SearchString, ReplaceString)

set OldDelims to AppleScript's text item delimiters


set AppleScript's text item delimiters to SearchString


set newText to text items of theText


set AppleScript's text item delimiters to ReplaceString


set newText to newText as text


set AppleScript's text item delimiters to OldDelims


return newText


end searchReplace





on run {input, parameters}


set myClip to the input


set mytext to searchReplace(myClip, "<", "")


set mytext to searchReplace(mytext, ">.", "")


set mytext to searchReplace(mytext, ">", "")


set mytext to searchReplace(mytext, "smb://", "\\\\")


set findIt to "/"


set replaceIt to "\\"


set mylocation to searchReplace(mytext, findIt, replaceIt)


set mylocation to "S:\" & mylocation








return mylocation


end run

(no idea why the last script is double spaced, its not in automator for me)

Additonally i'd like the script to open the finder location of the file, for the Windows to Mac path. I'd like the whole script to run from a droplet on the desktop the path is dragged into rather than from the services menu because various chat programs don't support services in the right click menu so if im set a path in a chat conversation with these scripts i have to paste into a TextEdit doc to convert, a step i'd like to remove.

Thanks
 

chown33

Moderator
Staff member
Aug 9, 2009
10,990
8,874
A sea of green
In the 2nd script, at this line:
Code:
set mylocation to "S:\" & mylocation

It looks to me like you're missing the 2nd backslash. As posted, the single backslash escapes the quote that follows it, which makes it an unterminated string.

Look two lines above the given line, and you have the correct way of dealing with backslashes. That is, to represent a single backslash, you type in two backslashes.

I didn't do any other code inspection, testing, etc. so there may be additional errors.

In general, when you post code that causes an error, it is often useful diagnostic information if you post the complete text of any error message. This lets us see what you've seen, without needing to run the code.
 
Last edited:

pierre1610

macrumors regular
Original poster
Feb 3, 2009
185
19
In the 2nd script, at this line:
Code:
set mylocation to "S:\" & mylocation

It looks to me like you're missing the 2nd backslash. As posted, the single backslash escapes the quote that follows it, which makes it an unterminated string.

Look two lines above the given line, and you have the correct way of dealing with backslashes. That is, to represent a single backslash, you type in two backslashes.

I didn't do any other code inspection, testing, etc. so there may be additional errors.

In general, when you post code that causes an error, it is often useful diagnostic information if you post the complete text of any error message. This lets us see what you've seen, without needing to run the code.
Thats, that does help that line. I do still get this error if i try the following line.

error

The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)”

line
Code:
            set mylocation to searchReplace(mylocation, "Volumes\Storage\Dropbox\Live\", "S:\\")
 

chown33

Moderator
Staff member
Aug 9, 2009
10,990
8,874
A sea of green
The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)”

line
Code:
            set mylocation to searchReplace(mylocation, "Volumes\Storage\Dropbox\Live\", "S:\\")
You need slashes (/) in the first string, not backslashes (\). Look carefully at this string and think about what you want to represent:
"Volumes\Storage\Dropbox\Live\"
 

pierre1610

macrumors regular
Original poster
Feb 3, 2009
185
19
You need slashes (/) in the first string, not backslashes (\). Look carefully at this string and think about what you want to represent:
"Volumes\Storage\Dropbox\Live\"
the (/) slashes have already been replaced earlier in the script with (\) backslashes by the lines
Code:
set mytext to searchReplace(mytext, "smb://", "\\\\")

set findIt to "/"

set replaceIt to "\\"

I've also tried to add an additional line
Code:
set mylocation to searchReplace(mytext, "Volumes\Storage\Dropbox\Live\", "")
it seems anything with a (\) in the searchReplace causes an error
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,990
8,874
A sea of green
...
I've also tried to add an additional line
Code:
set mylocation to searchReplace(mytext, "Volumes\Storage\Dropbox\Live\", "")
it seems anything with a (\) in the searchReplace causes an error
The code you're showing as the additional line has a literal string with unescaped backslashes in it. A literal string with backslashes in it as shown is simply not going to work. It's syntactically incorrect.

If you want a literal string with backslashes in it, then it must be typed in like this:
"Volumes\\Storage\\Dropbox\\Live\\"

It's very important that you understand what a backslash signifies in an AppleScript literal string. A backslash signifies the start of a two-character escape sequence. That is, a backslash means that the immediately following character has a special meaning, other than its literal significance.

When I do a web search for keywords applescript backslash these are two top hits:


You may also benefit from an AppleScript tutorial that shows how AppleScript works with strings, especially string literals.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.