osascript argv and records

On tiger, I have been trying to execute an applescript from the command line and passing a record in argv. I can pass data fine using

osascript x.scpt 'fred'

in x.scpt

on run argv
set x to item 1 of argv

x is set to "fred"

but what I need to do is something like

osascript x.scpt '{add:"domain.com",from:"Fred"}'

but x is never set to the record {add:"domain.com",from:"Fred"}

All help is appreciated.

Tom

powerbook, Mac OS X (10.4.3)

Posted on Dec 5, 2005 7:23 AM

Reply
4 replies

Dec 5, 2005 8:44 AM in response to Tom Shaw

osascript will pass your argument to the script as a string no matter what. If you want that string to then become something fancy like a record, you're going to need to clean things up a bit and hack about.

It's always a good idea to check and make sure your record labels don't cause terminology conflicts. In this case from is a commonly used preposition. To use it as a label we must pipe it such that it becomes |from|.

Since a record on its own passes for legal code, we can take advantage of the run script command's ability to compile plain text and return a result. It's not the fastest thing, but a step up from passing strictly positional parameters anyway.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">-- osascript test.scpt '{add:"domain.com", |from|:"Fred"}'
on run argv
set x to run script (item 1 of argv)
say |from| of x
end run</pre>

Dec 13, 2005 11:38 AM in response to ra5ul

what is x getting set to, e.g., with:

osascript x.scpt
\{add:\"domain.com\",from:\"Fred\"\}

?

ibook g4
OSX.4.x


Doesn't work 😟 with or without quotes

$ osascript SetUser2.scpt "\{username:\"test2\",domain:\"test.com\"\}"
SetUser2.scpt:94:104: execution error: Server X got an error: Can't make some data into the expected type. (-1700)

$ osascript SetUser2.scpt \{username:\"test2\",domain:\"test.com\"\}
SetUser2.scpt:94:104: execution error: Server X got an error: Can't make some data into the expected type. (-1700)

SetUser2.scpt:

on run argv
tell application "Server X"
set t to item 1 of argv

--try
set x to Set User t
--on error
-- return false
--end try
end tell
return x
end run

Dec 13, 2005 6:03 PM in response to Tom Shaw

The manpage for osascript is pretty explicit about arguments/parameters:
Any arguments following the script will be passed as a list of strings

(my emphasis) Passing arguments has only been around since 10.4 anyway. If there is sufficient demand to expand the feature, then maybe they'll do it. I think the method I suggested works pretty well, especially considering that you'd be compiling from plain text anyway. If you need more speed, you might look at script objects or using a drop file or something... else.

PowerMac G5 (June 2004) 2x1.8GHz 1.25GB, PowerBook G4 (12-inch DVI) 1x1GHz 768MB Mac OS X (10.4.3)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

osascript argv and records

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.