• 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
Using Home Dir Notation in Do Shell Script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using Home Dir Notation in Do Shell Script


  • Subject: Using Home Dir Notation in Do Shell Script
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 12 Apr 2013 19:11:36 -0500

Hey Folks,

There's no reason not to use home directory notation, but there are a few pitfalls.

This is how I used to do it:

set _dir to "~/Documents/Code Projects/"
set fullDir to quoted form of (do shell script "echo " & _dir)

Even though the home directory notation path has a space in it, it will produce the correct full-path when echoed.

But that full-path will fail when used in a shell script (due to the space) unless properly quoted (hence quoted form of).

The trick when quoting a home-dir path is to quote after the initial tilde-slash

    ~/'Documents/Code Projects/'

And this of course could be written differently:

    ~/Documents/'Code Projects'/
    ~/Documents/Code\ Projects/

It can get really messy if there are embedded quotes in the path:

set _dir to "~/Desktop/Code's \"Projects\"/"
set fullDir to quoted form of (do shell script "echo " & _dir)
--> ERROR:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

So I finally wrote a handler to manage this for me (see below):

do shell script "echo " & qt(_dir)
--> /Users/myHomeDir/Desktop/Code's "Projects"/
OR

do shell script "ls " & qt(_dir)

More follows demonstrating some of what works and what doesn't work.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
#  Input: a home directory based path ~/<path>, a full posix path, an alias, or a string.
# Output: a quoted posix path or a quoted string.
-------------------------------------------------------------------------------------------
on qt(_data)
if (class of _data) = alias then set _data to POSIX path of _data
if _data starts with "~" then
if length of _data > 2 then
tell _data to (text 1 thru 2) & quoted form of (text 3 thru -1)
return result
else
return _data
end if
else
return quoted form of _data
end if
end qt
-------------------------------------------------------------------------------------------

# Create test folder.
set _folder to ((path to desktop folder as text) & "Code Projects:")
tell application "Finder"
if not (folder _folder exists) then
set newFl to make new folder at desktop with properties {name:"Code Projects"}
set label index of newFl to 2
reveal _folder
end if
end tell

set tildePath to "~/Desktop/Code Projects/"
set fullPath to do shell script "echo " & tildePath
--> "/Users/myUser/Desktop/Code Projects/"

do shell script "open " & tildePath
--> The file /Projects does not exist.

do shell script "open " & quoted form of tildePath
--> The file /~/Desktop/Code Projects does not exist.

tell tildePath to (text 1 thru 2) & "\"" & (text 3 thru -1) & "\""
set tildePath to result
do shell script "open " & tildePath
--> Works

do shell script "open " & fullPath
--> The file /Projects does not exist.

do shell script "open " & quoted form of fullPath
--> Works

do shell script "open " & qt(tildePath)

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

set _text to qt(text 2 thru -2 of "
01 Now is the time for all good men to come to the aid of their country.
02 Now is the time for all good men to come to the aid of their country.
03 Now is the time for all good men to come to the aid of their country.
")

set _dir to "~/Desktop/Test Path with Spaces/"
set cmd to "
P=" & qt(_dir) & ";
mkdir \"$P\";
open \"$P\";
"
do shell script cmd

set _file to _dir & "Text File.txt"

set cmd to "echo " & _text & " > " & qt(_file)
do shell script cmd

set _file to _dir & "Ferrari.html"

set _url to "http://members.aceweb.com/randsautos/photogallery/ferrari/enzo/"
set cmd to "curl -L --user-agent 'Opera/9.70 (Linux ppc64 ; U; en)' " & _url & " -o " & qt(_file)
do shell script cmd

 _______________________________________________
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

  • Prev by Date: Re: do shell script, curl and cookies
  • Next by Date: Edit Message from Mail.app in BBEdit
  • Previous by thread: File Location by Reference
  • Next by thread: Edit Message from Mail.app in BBEdit
  • Index(es):
    • Date
    • Thread