Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Mac to UNIX path conversion AppleScript Apps
Now that we have this great ability to send AppleScript commands to the terminal, I find that it is a royal pain to convert the Finder path to a Unix style path. This is especially true when you have multiple volumes. This script has the "unix_path" subroutine that takes a Mac path and converts it to a UNIX path while taking into account just about every weird character you can throw at it.

As an example, it will display the Mac and UNIX path of the current Finder item, then open a terminal window and give a file-listing of the current finder-item.

Read the rest of the article for the Mac to UNIX path conversion script -- you'll have to enter this into ScriptEditor and save it in order to use it, of course.


on run
tell application "Finder"
set this_item to the selection as alias
end tell
display dialog of (this_item as text)
set mypath to posix_path(this_item)
display dialog of mypath
set the clipboard to mypath
end run

on posix_path(mac_path)
set mac_path to (mac_path as text)
set root to (offset of ":" in mac_path)
set rootdisk to (characters 1 thru (root - 1) of mac_path)
tell application "Finder"
if (disk (rootdisk as string) is the startup disk) then
set unixpath to "/" & (characters (root + 1) thru end of mac_path)
else
set unixpath to "/Volumes:" & mac_path
end if
end tell
set chars to every character of unixpath
repeat with i from 2 to length of chars
if item i of chars as text is equal to "/" then
set item i of chars to ":"
else if item i of chars as text is equal to ":" then
set item i of chars to "/"
else if item i of chars as text is equal to "'" then
set item i of chars to "\\'"
else if item i of chars as text is equal to "\"" then
set item i of chars to "\\" & "\""
else if item i of chars as text is equal to "*" then
set item i of chars to "\\*"
else if item i of chars as text is equal to "?" then
set item i of chars to "\\?"
else if item i of chars as text is equal to " " then
set item i of chars to "\\ "
else if item i of chars as text is equal to "\\" then
set item i of chars to "\\\\"
end if
end repeat
return every item of chars as string
end posix_path

    •    
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[17,199 views]  

Mac to UNIX path conversion AppleScript | 3 comments | Create New Account
Click here to return to the 'Mac to UNIX path conversion AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Script updated!
Authored by: robg on Nov 16, '01 10:05:15AM
Just a quick note to let everyone know that James has updated the script to handle even more naming conventions. The new script is reflected in the hint, and here's what James had to say about it:
Okay, I have improved the Mac to Unix path translation subroutine. I notice that there are some scripting plugins that provide this feature,but none of them are nearly as robust as mine. I believe that this new one can handle just about every weird character imaginable. Here is a list of the special characters it now handles: / ' " * ? [space] Of coure, it also handles being in a sub-volume. It also accepts the input to be in text or alias format (for simplicity). I probably really need to make this a scripting addition so that you don't have to update all existing scripts. Please contact me if somebady breaks this script. In this example, it will display the mac and unix paths of whatever is selected in the Finder. It will also send the unix path to the clipboard.


[ Reply to This | # ]
Mac to UNIX path conversion AppleScript
Authored by: balefrost on Sep 16, '08 04:55:15PM

Recent versions of OSX (using 10.5.5 currently) allow you to use "POSIX path of <item>". See Apple's documentation. In addition to what Apple says, it seems to work for forms such as:

POSIX path of "HD:Users:me:Documents:Welcome.txt"

(note that I don't convert the path into a file first, I'm taking POSIX path of <text>)



[ Reply to This | # ]
Doesn't handle special characters well
Authored by: ClassicUser on Oct 24, '08 09:09:22PM

Thanks for the heads-up on this new addition to the AppleScript dictionary, balefrost!

Unfortunately, though, the "POSIX path of " command doesn't handle several special characters well. For example, spaces are NOT delimited - making the resulting path nearly useless (unless the user were to manually delimit within quotes, for example).

As such, I still prefer the original script, with its manual recreation of this functionality. Kudos (again) to James for this very-useful addition - which I've added as a contextual menu item to ensure direct access.



[ Reply to This | # ]