I am always confused when it comes to declaring handlers to pass information into and out of the handler.
I am trying to put a phrase into a handler and I have been trying this for lack of a better way.
on process(the_args)
tell application "iTunes"
set name of result to my GetTitleFromFile() -- Not 'name of thisfile'.
end tell
end process
on GetTitleFromFile(usedtitle)
set finaltext1 to "What is Normal"
end GetTitleFromFile
I just can’t seem to get this to work.
in the set name command if I don’t put anything () I get that it doesn’t conform
if I put a variable name in the parenthesis (tname) it asks me to declare it
when I declare it set tname to “”
all I get back is “”
What do I need to do to get “What is Normal” back?
Not quite sure what info you are passing to the handler, if any. If all you want back is a consistent phrase just declare a property or variable (whichever is appropriate) at the beginning of your script. If you’re trying to learn handlers, there’s gotta be some good info here or on Apple’s site?
Anyway, in your case, this should return “What is Normal” from your handler
process("")
on process(the_args)
tell application "iTunes"
my GetTitleFromFile("") -- Not 'name of thisfile'.
end tell
end process
on GetTitleFromFile(usedtitle)
set finaltext1 to "What is Normal"
end GetTitleFromFile
property OutHere : 2
global NoStarter
local ScriptLevel
set MyResult to Double(5)
Double(Double(Double(Double(5))))
set SuperResult to result
on Double(Value)
set Answer to Value + Value
set OutHere to Answer + 3
set NoStarter to 5
set InsideOnly to Answer + 1
set ScriptLevel to Answer + 7
return Answer
end Double
--Variables at script level
OutHere -->> 13 set from within handler, best to use property
NoStarter -->>5 set from within handler
MyResult -->>10 The Returned Value
SuperResult -->>80 The Result processed 4 times
--Variables Not at script level and with error
ScriptLevel --Not Defined as nothing set yet
InsideOnly --Not Defined because it only inside handler
Answer -- Not Defined because it only inside handler
Thank you, that is a very illuminating script and I’m picking it up although the examples I’ve seen are just a single variable.
I’m still confused about a few things. In my example {theArgs} comes from an application and I have no idea what is in them. How can I find out?
I’m assuming this is like a handler but it seems that the on process(theArgs) must come as the first line from the application to pass the information on.
Is this correct?
All the examples you have shown are for one value. It gets complicated for me when things are lists or are passed in and out of a autmator function {input,parameters} how do I know what is passed to me? Also, when passing a string of text, is it the same? what about text items?
How do you get just the first variable of a statement declared like this searchReplace(ttitle,“hello world”,“”).
Thank you. This is much better than the sources I was using however I still have two problems which still prevent me from finishing my script.
The on process(TheArgs) command handler comes from an application and I don’t know what is required. In the automator command handler there are parameters and I am not sure how to find out what the necessary parameters are. Also, I am assuming from my trial and error that the handler on process must come first in my script. This means I don’t have the ability to declare properties or globals. Am I missing something?
I don’t have automator, but it might run something like this.
Script objects run something like scripts. If you run the script object with parameters the parameter is always sent as a list. If you use a string as a parameter, then it is coerced to a list of one item (the string). e.g.
script S
property some_prop : "anything"
on run p
return p
end run
end script
--
run script S with parameters "hello"
→ {“hello”}
Since you always want the first item of the passed list, you get the first item.
script S
property some_prop : missing value
on run p
set first_item to item 1 of p
return first_item
end run
end script
--
run script S with parameters "hello"
→ “hello”
If you send a list of parameters, you can use a repeat loop to use the parameters:
script S
property some_prop : missing value
on run p
repeat with this_parameter in p
display dialog this_parameter
end repeat
end run
end script
--
run script S with parameters {"hello", "bye"}
set v to {1, 2, 3}
try
error number -2720 from v
on error err_msg
-- "can't both consider and ignore ", 31
set list_text to text 32 thru -1 of err_msg
display dialog "The value of v is " & list_text
end try
beep 3