I am making a calendar alarm workflow that pulls text from a website and compares it to the text stored in a local file once a day. I store the text in the two variables "newText" and "oldText" in Automator. With the following apple-script code I try to access and compare these two variables. If they are equal, I want the break out of the workflow.
on run {input, parameters}
set newText to value of variable "newText" of front workflow
set oldText to value of variable "oldText" of front workflow
if newText is equal to oldText then
tell me to quit
end if
end run
The workflow works fine when run from automator, but when launched from the calendar-event I get the following error (second line):
Syntax Error, Expected end of line, etc. but found “"”.
All suggestions appreciated!
Outside of Automator you must wrap the related code in an application tell block
tell application "Automator"
set newText to value of variable "newText" of front workflow
set oldText to value of variable "oldText" of front workflow
end tell
if newText is equal to oldText then
tell me to quit
end if
Related
PreNote: I am open and hungry for any information, advice, tip etc.
Hello Everyone!
I am trying to create automation with applescript. This is my first personal applescript task but I have some valuable questions. Basically I am trying to catch live notifications from a website and display them in mac os notification.
I am trying to build process for a few days but I don't want to give a mess to you :) so I have roughly explained my process below.
(* Variables used in whole process
set $webToCheck > This is Safari webpage which I want to run my script on it. It won't be front window, script should be run with its name or other property.
set $theClass > This is class name of DOM element to check if it is exist or not. This class is not always exist on DOM of $webpage. It comes with notifications so when use it in "do Javascript" I got error "variable is not defined"
set $num > number of class to use in "do Javascript"
set $input > variable to assign HTML text
set $modifiedInput > Text of input seperated from HTML tags
*)
-- Step 1
tell application "Safari"
work on $webToCheck
-- Step 2
repeat until $input is not empty
set input do Javascript
document.getElementsByClassName > $theClass, $num of $webToCheck
end repeat
-- Step 3
modify text of $input to seperate from RAW HTML -- For example: <a class="" value=""> TEXT to be seperated </a>
Display notification $modifiedInput
-- Step 4
Go back to step 1 or 2 to check and display notification again
First of all, here are some general tips though:
Applescript won't accept $ at the start of variable names.
The variable assignment you are looking for is set {variable} to {value}. You can optionally at the end of it clarify the variable's class using as {class} at the end of the assignment.
Focusing a certain website does not happen with work on {URL} but as with most object oriented things in Applescript with the tell-statement. It will be shown in the full solution.
Text concatenation in Applescript happens with &. So something like "Hello " & "World" is the standard way to do it.
Modification of most things in Applescript happens with set.
It is easier to use innerText instead of innerHTML as splitting text in Applescript is a bit of a pain.
There is no goto but you could wrap the first few steps into a function, which are declared with on or to in Applescript.
Here is the full code with some documentation sprinkled in there:
global webToCheck, theClass, num, input --This says that all variables can be used even in functions.
set webToCheck to "youtube.com" --Strings can only use double quotes.
set theClass to "style-scope yt-alert-with-actions-renderer" --I will use an actual demo to prove that it is working
set num to 0 as integer -- This is the type declaration I was talking about. For numbers we have integer, real(float) and number.
set input to "" -- You don't have define everything at the top, but I will do so for this.
on displayNotification()
tell application "Safari"
tell window 1 -- This will target only the first window. For multiple windows you would have to write a repeat with-loop(for-loop), which I'm not going to do, for the sake of simplicity.
tell (first tab whose URL contains webToCheck) -- This targets just the first tab which contains the webToCheck variable.
set input to do JavaScript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerText" -- This is the way I would go about writing the Javascript. I think you had something different in mind, but this works for my example.
display notification (paragraph 1 of input) with title webToCheck -- This displays the first line of my input since that is the relevant part. I also set a title so I doesn't just say "Script Editor"
end tell
end tell
end tell
end displayNotification
repeat 4 times -- I think this is quite obvious. Adjust this to your needs.
displayNotification()
delay 4
end repeat
Running this while having not used youtube on Safari in a while it displays this:
Note that this isn't the most elegant solution, but I think it is readable and it (hopefully) works for your needs.
My AppleScript examines an image file and outputs some of that information. I need this information as normal output (stdout) of the script for further processing (in an InDesign script). But as soon as I open an image file in my AppleScript, the global variable which is meant to hold the result, gets lost.
This is fine (but useless):
global result
set result to "initialised"
set imgfile to POSIX file "/Users/hell/Pictures/interesting_stuff.jpg" as alias
tell application "Image Events"
-- start the Image Events application
launch
set result to "new text"
end tell
return quoted form of result
The following script is what I want, but throws the error "The variable 'result' is not defined (-2753)"
global result
set result to "initialised"
set imgfile to POSIX file "/Users/me/Pictures/interesting_stuff.jpg" as alias
tell application "Image Events"
-- start the Image Events application
launch
-- open image file
set img to open imgfile
-- here would go some commands about img instead of "new text"
set result to "new text"
-- close image file
close img
end tell
return quoted form of result
What is the problem here and how can I get the result out of my script?
result is a special identifier in AppleScript, which you may notice in Script Editor is printed in a different colour/font/style to other variable names you use. It's an AppleScript property that always contains the value returned by the command that is executed immediately before it.
The solution is most likely to choose a different word other than result for your identifier.
As your script stands, you also don't need to declare the variable as a global, since there are no scoping issues in your script that require access to variables outside of its block, which becomes a consideration when creating handlers or script objects.
I have an filemaker Pro 13 database with around 120 records (it's for a conference). I want to team it up with BBEdit to create individual files for each abstract, thus applescript. Much to my surprise (and despite a lot of web tips on scripting) '[tag:current record]' is not recognised in the script.
The relevant bit is this:
FM Script:
Loop
Perform Applescript
tell application "FileMaker Pro"
activate
set MyFileName to cell "WebAbstractFileName" of table "SelectionProcess"
set MyWebAbstract to cell "WebAbstract" of table "SelectionProcess" as text
end tell
-- (BBEdit bit, which works fine in testing)
Go to Next Record (exit after last)
End Loop
This works fine if I only want to retrieve the first record!
This applescript is set within a filemaker script which loops through the records but the script doesn't care which record it's in.
I've tried adding 'of current record' before the table reference but it then gives me errors (eg error "FileMaker Pro got an error: Object not found." number -1728 from cell "WebAbstractFileName" of current record of table "SelectionProcess") Without 'current record' it works fine, but only gives me the first record.
Here's (roughly) how you could do this in a Filemaker script:
Go to Layout [ “YourTable” ]
# FIND THE RECORDS OF INTEREST
Perform Find [ Restore ]
Go to Record/Request/Page [ First ]
Loop
New Window [ ]
# ISOLATE THE CURRENT RECORD
Show All Records
Omit Record
Show Omitted Only
Set Variable [ $path; Value:Get ( DocumentsPath ) & "someFolder/" & YourTable::Somefield & ".html" ]
Export Records [ No dialog; “$path” ]
Close Window [ Current Window ]
Go to Record/Request/Page [ Next; Exit after last ]
End Loop
This will export every record in the found set as an individual file into the folder "someFolder" located in the user's Documents folder, using the contents of the YourTable::Somefield field as the filename.
If, as you say, you don't need the separate files, then of course this can be much simpler.
More tinkering solved this. The crucial bit was to change the syntax. The script now reads:
tell application "FileMaker Pro"
activate
tell current record to set MyFileName to cell "WebAbstractFileName"
tell current record to set MyWebAbstract to cell "WebAbstract"
end tell
What seems to happen is that the fields must be visible (yet I had that not be a problem at one point...go figure. If they're visible, you can drop the table specification). This script, wrapped in a Loop block, will act on the found set and (if instructed) exit after the last record.
I append the bbedit script to create a new file and save it with a variable taken from another field in case it's of interest.
tell application "BBEdit"
set notesPath to ":Users:ophiochos:Dropbox:TL Conference Admin:Webpage materials:Abstracts:"
set newFilePath to notesPath & MyFileName & ".html"
set newDoc to make new text document with properties {contents:MyWebAbstract}
tell newDoc
set source language to "HTML"
save to newFilePath
close window
end tell
end tell
Or you could simply create a calculated field that contains the contents of the desired export file for each record, loop through the records one by one, and just use an Export Field Contents ['YourCalculatedField_c'] script step.
You can also use FM to preview the html output by using a web viewer to display your html. This, along with exporting individually, you can get all this functionality without the need for an external program.
If you need to change the text encoding for output files, you can also specify those in an xslt for outputting files:
http://filemakerhacks.com/2012/09/23/export-field-contents-as-utf-8/
Also, if performing applescript from within FileMaker, the "tell application" line is not needed since it is implied.
I'm trying to do the simplest thing in the world - a basic iterator in Automator. The workflow goes:
Get Value of a Variable (initially set to 1)
Run Applescript:
on run {input, parameters}
set input to input + 1
return input
end run
Set Value of a Variable
Loop
It works the first time, moving from 1 up to 2 as expected. But it fails on the second pass, giving the error
Can't make {} into type number. (-1700)
I'm clueless as to why - I've tried getting it to output from the Applescript as an integer and it makes no difference. Can anyone shed some light?
Your error is because on the second loop of your workflow your applescript is not receiving any input. I would guess that your loop function is not receiving any input and therefore it is not passing anything back into the applescript. Whatever is between your applescript and the loop function must be interfering somehow.
As an alternative, try this as your applescript. Your automator workflow should only have 2 actions, this applescript code and the loop action set to "use current results...".
In this code, on the first loop there won't be any input to the applescript so it will ask you for input, and then on subsequent loops the applescript will receive input from the loop action and thus it will increment your initial input.
Good luck.
on run {input, parameters}
if input is {} then
display dialog "Enter a number" default answer "1"
set input to (text returned of result) as number
else
set input to input + 1
end if
return input
end run
I am trying to create a service in OSX leopard that counts the number of words of selected text. I have automator set to run an applescript, with the following put in it:
on run {input, parameters}
count words of input
display alert "Words: " & input
return input
end run
When I compile the script, it says it cannot count every word. What am I doing wrong?
Thanks for the help,
Elliott
First of all, I presume you are testing this in Automator, and that's where the error is taking place? If so, the likely problem is that there is no input—so it can't count the words of nothing. In order to test it successfully, you need to temporarily add a "Get Specified Text" action before the Run AppleScript action, and enter some test text into that field. You'll have to remove the Get Specified Text action before using it as an actual service.
Secondly, you need to use
count words of (input as string)
in order to get a proper count, otherwise it'll return zero.
I made one here, on Github:
https://gist.github.com/1616556
The current source is:
on run {input, parameters}
tell application "System Events"
set _appname to name of first process whose frontmost is true
end tell
set word_count to count words of (input as string)
set character_count to count characters of (input as string)
tell application _appname
display alert "" & word_count & " words, " & character_count & " characters"
end tell
return input
end run
Use Automator.app to create a new service, and then select the Run AppleScript action. Paste this code in to the text box, and save as Word and Character Count. Now switch to a new app, select some text, and open the context menu to find the new option.