Automating System Profiler

This has to be my biggest gripe about AppleScript: how do you pass arguments to a script without resorting to file IO? osascript surely doesn’t provide that.

File-based argument passing stinks because:

  1. slow
  2. the filenames must be determined before calling the script - this has implication especially when the script could be run simultaneously by more than one callers.

Because this is such an obvious issue, there must alredy exist some solution… please share with us, thanks!

less than 10 min after I posted the question, I found you can use “system attribute” to get the environment vairables! This might be the solution… Anything better than this?

You can pass arguments to any handler inside a script:

set theScript to (load script alias "path:to:whatever.scpt")
tell theScript to doSomething("Mr. Floppy")

Where the script “whatever.scpt” contains such handler:

to doSomething(x)
	say "Welcome, " & x
end doSomething

I was thinking about calling osascript from my shell script and how to pass arguments from shell script to osascript. But your post did help - it reminded me I can just do an ‘osascript -e’ and stuff whatever argument I want to pass in in the commands. To return argument to a shell script, I can do a

set x to “my result”

and upon return from osascript it should print “my result” back to stdout and I can grab that via pipe.

Thanks!

I’m new to AppleScript, but I’m sort of an old UNIX and bourne shell script hacker,
so I came up with something that seems to work:


#!/bin/sh
# Header to implement command-line arguments AppleScript.
case `uname` in
Darwin) shopt -s xpg_echo ;;
*) 1>&2 echo "Sorry, this only runs on Darwin (MacOS X)."; exit ;;
esac
# This line causes the rest of the script to be executed as AppleScript.
(
  echo "set argv0 to 42$042"
  if [ $# -gt 0 ]; then
    echo "set argv to { 42$142c"; shift
    while [ $# -gt 0 ]; do
      echo ", 42$142c"; shift
    done
    echo " }"
  fi
  awk 'NR > 18 { print }' $0            # NR > (this line number) + 1
) | osascript; exit
-- From here on, comments must be preceded by '--'.
set av to { argv0 } & argv
return av

Not that some of the code at the top is necessary for me since I use several
different varieties of UNIX. Most of my shell scripts work on all varieties, but
of course this one won’t.

I’ve checked this with things like ‘test1 a b “c d”’ and it seems to honor the
fact that there are 3 arguments, and the 3rd has spaces.

Now I just have to find out how to get STDOUT of an AppleScript, and, well,
about a million other things, as I just started AppleScript a few days ago.

Wayne

I don’t know exactly how does work osascript, and if it can return results from the last run. But you can allways make the applescript code to write some text to a temp file which you can “cat” after execution.

Same here - I came from UNIX-land and just started AppleScripting about a month ago…

Yes the method you posted works: for me I just use this simple pattern:

AS_ARG=“my arg”
osascript myScript.scpt

[edit: i mixed up shell script and applescript :wink: ]

There is an implicit limit though: the max length of environment variables.

The “load script” trick suggested by jj above doesn’t have this limit, as you can set a string to the following two lines in your shell script:

set theScript to (load script alias “path:to:whatever.scpt”)
tell theScript to doSomething(“Mr. Floppy”)

Obviously you need to replace “Mr. Floppy” to the content of the arg you want to pass. And then you call “osascript -e” with the quoted form of the string!

As to getting the output from osascript, I’m using Python to do shell scripting, so it’s really easy to just use os.popen2() for that (pipes). But I’m sure other scripting languages give you that kind of facility too.

Scripting System Profiler?

Hello Scripters,

I’m trying to script the System Profiler to saves profiles of machines to a database. My System Profiler (4.0.3v40 on OSX Pnther 10.3.3) doesn’t create any files when i use the following script.

tell application "System Profiler"
	activate
	set cReport to make new document at beginning
	save cReport as Unicode text in file "shiva:Users:jochen:Desktop:MyProfile1"
	close cReport saving yes saving in file "shiva:Users:jochen:Desktop:MyProfile2"
	
end tell

Am i missing something? Is there something else that works?

thanks for suggestions.

jacques