Re: unix style paths
Re: unix style paths
- Subject: Re: unix style paths
- From: Roger_Jolly <email@hidden>
- Date: Tue, 16 Jul 2002 17:05:52 +0200
Hi Kris,
Why not just use:
set dest to (path to preferences from user domain)
I don't think you can use a Unix style path in Applescript directly, but it
is relatively easy to convert one to something Applescript can use.
I recently contributed the following sample routine (Get UTF8 Posix Path) to
ScriptBuilders on www.macscripter.net that does just that and also works
around the problem in the posix path command were it assumes that the shell
and Applescript are using the same charactersets and so will fail if a
filename contains a high ascii character. (The routine on macscripter comes
with a more detailed commentary.)
Hope this helps,
Roger
---
on run
tell application "Finder" to set TheFiles to selection as list
HandleFile(TheFiles)
end run
on open TheFiles
HandleFile(TheFiles)
end open
on HandleFile(TheFileList)
repeat with TheFile in TheFileList
set ItemPath to GetUTF8StylePath(TheFile)
display dialog "The posix path of: " & return & "\"" & (TheFile as
alias) & "\"" & return & "is:" & return & "\"" & ItemPath & "\""
set TheItem to GetItemFromPath(ItemPath)
display dialog "The alias of:" & return & "\"" & ItemPath & "\"" &
return & "is:" & return & "\"" & (TheItem as string) & "\""
end repeat
end HandleFile
on GetUTF8StylePath(AnItem)
if AnItem is desktop then
set ThePath to POSIX path of (path to desktop)
else
set ThePath to POSIX path of (AnItem as alias)
end if
do shell script "ls -d \"" & ThePath & "\"" -- see if the posix path
is the correct one
if result is "" then
-- use the url to produce the correct posix path
-- A url encodes non-ascii characters by using a percentage (%) sign
-- followed by two hexadecimal numbers.
-- The shell encodes filepaths as if these pairs of digits were
-- ordinary ascii characters.
tell application "Finder" to set TheURL to url of AnItem
set ThePath to ""
set i to 17 -- strip off "file://localhost" at the beginning of a
url
repeat while i > length of TheURL
if item i of TheURL is not "%" then
-- as long as you haven't found a "%", just add the
-- character to the path
set ThePath to ThePath & (item i of TheURL)
set i to i + 1
else -- when you find a "%", translate the following two numbers
-- into a ascii character, and continue with the rest of
-- the path
set TheHexNumber to item (i + 1) of TheURL & item (i + 2) of
TheURL
set ThePath to ThePath & (ASCII character
Hex2Dec(TheHexNumber))
set i to i + 3
end if
end repeat
end if
return ThePath
end GetUTF8StylePath
on Hex2Dec(TheNumber)
-- Just a general converter of hexadecimal numbers into decimal
set TheLength to length of TheNumber
set DecNumber to 0
repeat with i from 0 to (TheLength - 1)
set TheCurrentChar to (character (TheLength - i) of TheNumber)
if TheCurrentChar is in {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9"} then
set DecNumber to DecNumber + TheCurrentChar * (16 ^ i)
else
set DecNumber to DecNumber + ((ASCII number TheCurrentChar) -
55) * (16 ^ i) -- 55 = ASCII number "A" + 10
end if
end repeat
return DecNumber
end Hex2Dec
on GetItemFromPath(ThePath)
set TheFile to (do shell script "echo \"" & ThePath & "\" | tr :/ /:")
try
tell application "Finder" to set TheFile to item TheFile
return TheFile
end try
set Success to false
repeat while not Success
set Success to true
set TempAliasFile to "temp_" & time of (current date)
try
do shell script "ln -s \"" & ThePath & "\" /tmp/" &
TempAliasFile
on error theerror
set Success to false
end try
end repeat
tell application "Finder" to set TheFile to original item of file
TempAliasFile of item "tmp" of startup disk
do shell script "rm /tmp/" & TempAliasFile
return TheFile
end GetItemFromPath
on 15-07-2002 18:52, Kris Steinhoff at email@hidden wrote:
>
Hi,
>
>
Does anyone know how to make references to files with their unix style
>
path. such as "/Users/Shared/whatever"
>
>
I am trying to copy some preference files to a users
>
~/Library/Preferences folder and I want it to work for any user that
>
logs in. To do that I'll use 'system attribute "home"' and then
>
concatenate "/Library/Preferences" to the result. The reason I am not
>
using a shell script it that the preference files I am trying to copy
>
seem to be dual-fork (does that even make sense?) and using cp or even
>
ditto breaks the file.
>
>
here's what I need to do (this is just pseudo code, the syntax is
>
probably wrong):
>
>
set dest to (system attribute "HOME") & "/Library/Preferences"
>
copy [or move] "/private/Prefs" to dest
>
>
and applescript doesn't seem to know what to do with the unix style path
>
reference, so does anyone know how to use a unix style path in
>
Applescript?
>
>
thanks
>
-kris
>
_______________________________________________
>
applescript-users mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.