AppleScript check for string value in input - applescript

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.

Related

How to judge which language a input belongs to in AppleScript?

I want to add a item in Services on macOS such as:
I pick a word on webpage, then search its explain on Wikipedia.
If the word is english, i want to search "https://en.wikipedia.org/wiki/", if the word is chinese, i want to search "https://zh.wikipedia.org/wiki/". so firstly I need to judge the language of input. I want to how to do it in AppleScript. Thanks.
Determining the language of a piece of text can't be done in vanilla AppleScript as far as I know, but it can be done using AppleScriptObjC.
Although you weren't that keen on this revelation, nonetheless, I'm including a snippet that demonstrates it's actually pretty easy to do what you want:
to guessLanguage for input
script
use framework "Foundation"
property this : a reference to current application
property NSLTag : a reference to NSLinguisticTagger of this
to guessLanguage()
(NSLTag's dominantLanguageForString:input) as text
end guessLanguage
end script
result's guessLanguage()
end guessLanguage
You should place this handler at the bottom of your script, after end run. It can be called from anywhere within your script like so:
on run {input, parameters}
set [input] to the input
guessLanguage for the input
set lang to text 1 thru 2 of result
open location "https://" & lang & ".wikipedia.org/wiki/" & input
end run
to guessLanguage for input
.
.
.
end guessLanguage
Here are a couple of test runs on different linguistic samples, showing the returned result next to each of them:
guessLanguage for "Hello, how are you?" --> "en"
guessLanguage for "Guten Tag, wie gehts?" --> "de"
guessLanguage for "Salut, ça va?" --> "fr"
guessLanguage for "你好!你好嗎?" --> "zh-Hant"
guessLanguage for "こんにちは、元気ですか?" --> "ja"

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 subroutine

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.

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

How do I get the output of an Xcode user script to auto indent?

The Problem
I want to press a key when I have a line highlighted and convert from a single line:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1 to:date2 intoMOC:mockRawMOC];
to a multiline statement:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
What I've Tried
I've got a simple ruby script that almost gets me there.
#!/usr/bin/env ruby
s = STDIN.read
s.gsub!(/(:.+?\w) (\w.+?)/,'\1' + "\n\t" +'\2')
print s
When I set the output to "Replace Selection", I get this:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
When I set the output to "Place on Clipboard", then paste it in, I get the desired result:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
However, this is two keypresses which is clumsy.
Any ideas how I can get the replaced text to obey Xcode's auto indent rules?
Check the pre-installed script for "Convert tabs to spaces", and how it executes an in-line applescript. Use that to tell XCode to perform the menu item
Edit > Format > Re-Indent
I'm not sure how you do that with ruby, nor the details about the applescript content, but I would wager it's fairly straight-forward..

Resources