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

Combining AppleScript variables and UNIX shell scripts UNIX
For the past three months, I have been trying to run a shell script through an AppleScript. This is easy enough using the do shell script command, but I wanted to use a variable (entered from a dialog box) somewhere in that code. So, what i was trying to do was:
set thevariable to "bob.txt"
do shell script "ls ~/ >>~/Desktop/thevariable"

That didn't work, so I played around with it and I came up with this, which does work:

set thevariable to "bob"
set thescript to "ls ~/ >>~/Desktop/" & thevariable & ".txt."
do shell script (thescript)

Hope this helps someone...

    •    
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[13,409 views]  

Combining AppleScript variables and UNIX shell scripts | 7 comments | Create New Account
Click here to return to the 'Combining AppleScript variables and UNIX shell scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Combining AppleScript variables and UNIX shell scripts
Authored by: trekan on Aug 07, '03 10:38:37AM
Instead of

set thevariable to "bob.txt"
do shell script "ls ~/ >>~/Desktop/thevariable"


You should have done:

set thevariable to "bob.txt"
do shell script "ls ~/ >>~/Desktop/" & thevariable


[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: the_webmaestro on Aug 07, '03 10:45:02AM

I'd like to do this, but I need to select some files (using the Open dialog) first. Any ideas to help here?

---
Father of Jeremy Logan Leeds



[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: Arno on Aug 07, '03 11:06:27AM
set theVariable to choose file with prompt "Choose a file, please:"
do shell script "ls ~/ >>~/Desktop/" & thevariable


Et hop!

[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: kconboy on Aug 07, '03 11:11:54AM

Duh. This is basic scripting syntax (not just AppleScript), not really a MacOS X Hint. Why would you try and use the Desktop to hold a variable??

---
----
kev



[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: lee1 on Aug 07, '03 11:25:24AM

Check out

http://developer.apple.com/technotes/tn2002/tn2065.html

for a good discussion of related issues.



[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: robJ on Aug 07, '03 11:44:33AM
This will likely fail if the thevariable contains a space (bob smith.txt). I usually try to build qualified paths before passing them to the do shell script command. Here's some general info (snipped from a post on Apple's AppleScript forum) regarding paths sent to the shell.

The shell doesn't like traditional Mac paths such as:

"path:to:some:cool file"

It prefers POSIX paths.

POSIX path of "path:to:some:cool file"
Returns: "/path/to/some/cool file"

The shell also dislikes spaces in file/folder names. Using the 'quoted form', you can provide a path in quotes.

quoted form of "/path/to/some/cool file"
Returns: "'/path/to/some/cool file'"

You can put them together for cleaner code.

quoted form of POSIX path of "path:to:some:cool file"
Returns: "'/path/to/some/cool file'"

This will usually escape undesirable characters in a manner that is acceptable to the shell.


[ Reply to This | # ]
Combining AppleScript variables and UNIX shell scripts
Authored by: markuswarren on Aug 07, '03 02:14:35PM

I do agree it's pretty basic applescript stuff, however, some people don't know about it. heck I know a bit about applescript and had not see the 'quoted form' thing, which I'm about to use as i'm going to rewrite some scripts that deal with paths which will include spaces in file names.



[ Reply to This | # ]