• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Encoded file URL to Mac or Posix path...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Encoded file URL to Mac or Posix path...


  • Subject: Re: Encoded file URL to Mac or Posix path...
  • From: kai <email@hidden>
  • Date: Wed, 14 Mar 2007 01:10:59 +0000


On 13 Mar 2007, at 23:53, Peter Bunn wrote:

Since I asked the original question, it's pretty much implicit that I
know little about the subject...

But all the snippets offered so far (short of script additions) have only
handled MacRoman characters... and break on other international
characters...


... and I haven't yet tried the script additions.

It ain't exactly urgent, but if anyone has a 'cure', I'm all ears.

Are you sure that the snippets don't just *appear* to break with certain characters, Peter?


Remember that an application like Script Editor may not handle Unicode-only characters correctly. This means we can't define a file path containing such characters using our system's primary encoding - so we'd have to define it using either a Unicode encoding method, or something like the 'choose file' command.

It also means that the result of the URL-to-path conversion may not be displayed accurately in the Result pane - although we can always test the result by displaying a dialog (which does display Unicode correctly).

For example, assuming the file is on the desktop, Finder should be able to carry out a simple URL-to-path conversion for us:

-----------------

to get_file_url()
set file_name to «data utxt02870257026A0279025402820259026D025702570250»
tell application "Finder"
tell file file_name to if exists then return URL
(make file with properties {name:file_name})'s URL
end tell
end get_file_url


-- make a file (if necessary) and get its URL:
set target_url to get_file_url()

-- display the URL
display dialog ("File URL:" as Unicode text) & return & target_url buttons {"OK"} default button 1


-- convert the URL to path:
tell application "Finder" to set file_path to (first item whose URL is target_url) as Unicode text


-- test the actual result:
display dialog ("File path:" as Unicode text) & return & file_path buttons {"OK"} default button 1
--> should display path correctly


-- test the displayed result (path may appear incorrect):
file_path
--> "startupdisk:Users:username:Desktop:???????????"

-----------------

I suppose the same sort of approach could be extended to items in other locations:

------------------

on path_of_url(t)
set c to 0
set d to text item delimiters
set text item delimiters to "/"
tell application "Finder" to considering case
tell (get URL of disks) to repeat with i from 1 to count
tell item i to if t starts with it then tell (count (text items)) to ¬
if it > c then set {c, f} to {it, application "Finder"'s disk i}
end repeat
repeat with i from c to (count t's text items) - 1
set f to (f's first item whose URL starts with t's text 1 thru text item i)
end repeat
if t does not end with "/" then set f to f's first item whose URL is t
end considering
set text item delimiters to d
f as Unicode text
end path_of_url


choose file without invisibles

-- convert alias to URL
tell application "Finder" to set target_url to result's URL

-- display the URL
display dialog ("File URL:" as Unicode text) & return & target_url buttons {"OK"} default button 1


-- convert the URL to path:
set file_path to path_of_url(target_url)

-- test the actual result:
display dialog ("File path:" as Unicode text) & return & file_path buttons {"OK"} default button 1
--> should display path correctly


-- test the displayed result (path may appear incorrect):
file_path
--> "path:to:file:???????????"

------------------

However, for greater succinctness, I can't see much wrong with using a shell script - such as this variation:

------------------

choose file without invisibles

-- convert alias to URL
tell application "Finder" to set target_url to result's URL

-- display the URL
display dialog ("File URL:" as Unicode text) & return & target_url buttons {"OK"} default button 1


-- convert the URL to POSIX path:
set POSIX_path to do shell script "python -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' " & quoted form of target_url


-- test the actual result:
display dialog ("POSIX path:" as Unicode text) & return & POSIX_path buttons {"OK"} default button 1
--> should display POSIX path correctly


-- test the displayed result (POSIX path may appear incorrect):
POSIX_path
--> "path/to/file:???????????"

------------------

---
kai


_______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
References: 
 >Re: Encoded file URL to Mac or Posix path... (From: Peter Bunn <email@hidden>)

  • Prev by Date: Re: simple duplicate script question
  • Next by Date: [ANN] My demonstration movie of Pen-IT NOTES
  • Previous by thread: Re: Encoded file URL to Mac or Posix path...
  • Next by thread: Re: Encoded file URL to Mac or Posix path...
  • Index(es):
    • Date
    • Thread