Applescript subroutine - applescript

I have a helperScript which has a few basic functions that I frequently use.
My Current script’s flow goes like this:
on SubA()
Set HelperScript to load…..
tell HelperScript
: :
: :
end tell
end SubA
on SubB()
Set HelperScript to load…..
tell HelperScript
::
::
end tell
end SubB
on run paravlist
Set HelperScript to load…..
tell HelperScript
SubA()
SubB()
end tell
end run
I am unable to call SubA() and SubB() as the helper script is being set and used from each of subroutines. If I comment out the usage of helperScript. I am able to call subroutines from one another. What is the best way to deal with a problem like this? I want to use the helperScript in every subroutine.

After reading your question a few more times, I think I've figured out what you're asking. You're trying to load a script within your method and then you want to call a method that is within that script?
If that is the case, I think what you're looking for is this...
set HelperScript to load script...
set theResult to someMethod() of HelperScript
EDIT :
I'm still not clear if you have two scripts or one, so i've updated the answer to reflect both cases.
Dual script example...
property HelperScript : null
on run
try
if not loadScript() then error "Unable to load script"
set rslt1 to SubA() of HelperScript -- This approach assumes HelperScript.scpt is a different script and it contains a method called SubA
set rslt2 to SubB() of HelperScript -- This approach assumes HelperScript.scpt is a different script and it contains a method called SubB
on error errMsg
activate
display dialog "Error: " & errMsg buttons {"OK"} default button 1 giving up after 10
end try
end run
on loadScript()
try
set HelperScript to load script (POSIX file "/Path/To/HelperScript.scpt")
return true
on error
return false
end try
end loadScript
Single script example...
on run
try
set rslt1 to SubA() -- This approach assumes your HelperScript is THIS script
set rslt2 to SubB() -- This approach assumes your HelperScript is THIS script
on error errMsg
activate
display dialog "Error: " & errMsg buttons {"OK"} default button 1 giving up after 10
end try
end run
on SubA()
try
-- Do something here
return true -- or some other value
on error
return false -- or some other value
end try
end SubA
on SubB()
try
-- Do something here
return true -- or some other value
on error
return false -- or some other value
end try
end SubB

AppleScript has included a library loading system since 10.9. It's not great (e.g. avoid the SDEF garbage as it's 1. make-work and 2. bug-injector) but it generally does the job. I recommend you adopt that.

Related

How to pass the identity of the caller Script to the external Script Library?

How to best pass the identity of the caller Script to the external Script Library?
I have found a way to do this successfully ... however the question then becomes "Is it the best way?"
In the external Script Library, I have:
property pCallerScript : 0
on libPassCaller(theCallerScript)
set pCallerScript to theCallerScript
end libPassCaller
followed later in this Library, for example:
set topmostError to pCallerScript's gTerminateThisScript & return & return & localError
I set all this up via the following code in my Calling Script:
property pExternalLib : ""
set pExternalLib to load script file pExternalScriptFile
property passCaller : me
pExternalLib's libPassCaller(passCaller)
So, is this the very best way?

Applescript Objective C isn't outputting to variable

Using Xcode, I'm trying to pipe the output of a shell command that I use to check the list of available printers on a print server. It works in Terminal, but the output can only be seen in the debugger, and when I put the script into Script Editor, I get the output displayed as a "Syntax Error".
I'm trying to have it come up as an alert with the Display alert command but I've had no luck.
on Button_(sender)
set Printerlookup to "/usr/local/bin/iprntcmd --listprintersonserver printerserver.com"
set Printers to do shell script Printerlookup
display alert "The available printers are:" & Printers
end Button_
EDIT:
This is the whole appdelegate.
script AppDelegate
property parent : class "NSObject"
property txtBox: ""
-- IBOutlets
property theWindow : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
use scripting additions
on Button_(sender)
if txtBox is "" then
display alert "Please enter some text"
else
set iPrntlookup to "/usr/local/bin/iprntcmd --listprintersonserver "& txtBox &"printerserver.com"
set Printers to do shell script Printerlookup
display alert "The available printers are:" & Printers as text
end if
end Button_
end script
This is the expected output, which is found in the debugger area as opposed to the dialog box.
2018-05-07 21:34:12.925412+1000 iPrint Local[29523:430596] MessageTracer:
Falling back to default whitelist
2018-05-07 21:34:17.104407+1000 iPrint Local[29523:430596] *** -[AppDelegate
Button:]: iprntcmd v06.07.01
Listing printers on testprintserver.com.
ipp://testprintserver.com/ipp/printer1
ipp://testprintserver.com/ipp/printer2 (error 1)
Add a use scripting additions statement at the top of your script.

AppleScript check for string value in input

new to applescript and this small check is driving me nuts.
I want to check if the input to the script has the substring of "mob".
If i create a varible with mob1234, It works and returns true.
on run {input, parameters}
set testString to "mob1234"
display dialog {"MOB" is in testString}
return input
end run
If i change it to use the input, and set the input to mob1234, it fails and gives me false
on run {input, parameters}
set testString to input
display dialog {"MOB" is in testString}
return input
end run
I have no idea.
You don't say how you're invoking your script. If you're calling the script via Automator, be aware that in this mode input is a list, not a string, so this should work:
set testString to item 1 of input
Since the answer given by iayork seems to be right, here some follow up:
-- When this is saved as compiled script one can call it from Terminal like:
-- `osascript scriptName.scpt MOBstringArg1 arg2`
on run {input, parameters}
if (input's class is list) then set input to (item 1 of input) as text
if "MOB" is in input then
display notification "FOUND MOB" with title "In: " & input
end if
return input
end run
See comment from iayork about calling a script with osascript.
You can also just coerce the input to text directly when you assign it to testString: set testString to input as text, that is, if you aren't going to use any other list item of input.

Passing parameters to AppleScript causes error

I have an AppleScript file that I'm passing two values to and it is giving me this error:
execution error: {"file2"} doesn’t match the parameters {fileName, fileName2} for run. (-1721)
Here is the AppleScript:
on run {fileName, fileName2}
set output to fileName & "|" & fileName2
end run
UPDATE:
On further testing if I add more parameters it seems to work. Is there an arguments array or parameters array that I can use instead?
Your handlers requires two parameters and you are only passing one. Post your handler call.
You can use
on run argv
repeat with aParameter in argv
display dialog aParameter as string
end repeat
end run
The on run-argument (here called argv) is an Applescript list that contains the parameters.
Enjoy, Michael / Hamburg

What is the code to exit/ stop VBscript from running in the event of a condition not being met?

I have looked on Google and the answer is not there!
First things first. WScript.Quit DOES NOT WORK! I have no idea what "WScript" is but it clearly has nothing to do with client side scripting for a web page. I have seen this "WScript" thing somewhere before and it just produces errors (maybe obsolete or something) so please do not suggest it...
Anyway... all I wish to do is completely stop the script in the event of a condition not being met. Obviously I don't want "Exit Sub" because the code would then carry on running if that sub is embedded!
I am aware of the "stop" command but I am under the impression that it is only used for debugging.
Hopefully a very simple question.
UPDATE and Conclusion: Before I close this subject I will just expand a little on what I was trying to do...
I had a number of main subs that were being started by a button click. In order to make it so that I did not have to edit each individual sub I embedded a universal sub within each one that did a preliminary check.
Part of that preliminary check was to stop the program in the case of an incorrect user input. If an error was detected I wanted to halt all progress from that point on. An "exit sub" would obviously just skip the rest of that preliminary sub and the main sub would carry on executing.
In the end it was just a case of writing in an error flag (that is checked in the main subs) or incorporating the error condition operation in each main procedure. In that way you exit the main sub and the problem is solved.
It was not laziness - I just wanted to reduce the amount of code. Thank you for the responses anyway.
I've found that the WScript is always available if running a .vbs/.vbe/.wsf script using either the wscript.exe or cscript.exe engine. When WScript is not available is if running using a different engine. E.g. running VBScript within a HTA, a webpage, in VBA or from a hosted COM script control.
To exit a script which is not running from wscript.exe or cscript.exe, you can do something like the following:
main
Sub main
' execute code here
' oops a confition is not met:
If Not condition then Exit Sub
' more code to execute if condition was met
End Sub
You can use something like this:
Sub MyFunc
----------
My Code
----------
End Sub
Function Main
On Error Resume Next
MyFunc
If Err.Number <> 0
Exit Function
End Function
It'll stop executing the code, the point it finds an exception or throws an error.
The simplest way, What i found is: WScript.Quit
The Wscript object is only available if you are running in the Windows Script Host (wscript.exe,cscript.exe). NOT Internet Explorer (iexplorer.exe, mshta.exe)
Well an easy way to do this is to declare a global variable that gets set to false until an error occured, and if the error occured then that variable will be set to true, after that anytime that variable gets checked you can exit sub\function\for\do\whatever.
Example:
Dim ErrorOccured
On Error Resume Next
ErrorOccured=False
Sub Main()
If ErrorOccured Then Exit Sub
'some code
MsgBox "Main has run"
End Sub
Sub MakeAnError
If ErrorOccured Then Exit Sub
'some code
Err.Raise 2
If Err Then ErrorOccured=True
End Sub
Function TellMeRandom
If ErrorOccured Then Exit Function
'some code
Randomize
TellMeRandom =Int((100- 1+ 1) * Rnd + 1)*1
End Function
Function ResetError
ErrorOccured=False
End Function
Call Main 'Main will run because an error has not occured (ErrorOccured is False)
MsgBox TellMeRandom 'This will return a random number 1-100
Call MakeAnError 'This will set the Global ErrorOccured to true
MsgBox TellMeRandom 'This will be blank because the ErrorOccured prevented it from running
Call Main 'This will not run because the ErrorOccured prevented it from running
Call ResetError 'This will set the Global ErrorOccured to false
Call Main 'This will run because ErrorOccured is back to False
MsgBox TellMeRandom 'This will return a random number 1-100 because ErrorOccured is back to false
Just remember to Add If ErrorOccured then Exit Sub for a sub routine or If ErrorOccured then Exit Function for a function.
You could also raise an error such as: Err.Raise 507
This Error will exit your current script : "An exception occurred".

Resources