can i get correction on my Applescript error using else if...? - applescript

activate application "Logic Pro X"
tell application "System Events"
tell process "Logic Pro"
set tracks_window to title of first window whose title contains "- Tracks"
if value of static text 1 of group 3 of list 1 of group 2 of window tracks_window is "Track:" and value of UI element 1 of group 3 of list 1 of group 2 of window tracks_window is 0 then click UI element 1 of group 3 of list 1 of group 2 of window tracks_window
else if value of static text 1 of group 2 of list 1 of group 2 of window tracks_window is "Track:" and value of UI element 1 of group 2 of list 1 of group 2 of window tracks_window is 0 then click UI element 1 of group 2 of list 1 of group 2 of window tracks_window
end if
end tell
end tell
Hi I'm new to programming.
let's say first <if~> line as Code1 and second <else if~> line as Code2 for convenience.
what i've been trying to do was
if Code1 doesn't work, execute Code2
the results of Code1 and Code2 are same on GUI.
but as the app varies the index of UI elements by on its situations, I had to use complexed if/else if.
each of Codes are working good when run separately (on correct condition)
but not on one script combining them with else if~.
462:469:
syntax error: Expected “end” or “end tell” but found “else if”. (-2741)
this was the message I got from Visual Studio Code when I run them.
so I tried to figure out by edit stuffs like else, end if
but didn't worked as I intended.
could anyone can help me how to solve this error?
thanks.

Related

Applescript for Safari AUTOMATION -- any advice, information, tip etc --

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.

AppleScript - Syntax Error: Expected end of line but found """

I am a beginner in AppleScript and I am trying to code a script that will write data into a list directly from a Numbers table.
So I coded this function:
on readTableABC(sheetName)
set readTable to {}
tell table "Table 1" of sheetName
set rowCellCount to count of cells of column "A"
tell column "B"
repeat with i from 2 to rowCellCount
set the end of readTable to value of cell i
end repeat
end tell
end tell
return readTable
end readTableABC
sheetName will actually take the sheet name of the Numbers file.
When I compile, I get an error which says:
Syntax Error: Expected end of line but found """ .
And the cursor shows this character:
[Character pointed out by the compiler][1]
[1]: https://i.stack.imgur.com/VUqxa.png
Would you have any idea where the mistake would be?
Thanks a lot for your help.
Nicolas
As mentioned by red_menace:
In order for the scripting terminology from the Numbers application to be used, they need to be included in a tell application "Numbers" statement, or a use application "Numbers" statement needs to be declared.

Applescript: Batch save files in Preview without save dialog

thanks in advance for your help.
New to Applescript.
Trying to open in Preview all files in folder and save them.
The problem is that saving pops up a dialog half the time, which requires me to sit there hitting enter.
Code:
tell application "Finder"
set fl to files of folder POSIX file "/Users/myname/Desktop/myfolder/" as alias list
end tell
repeat with f in fl
tell application "Preview"
activate
open f
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
save front document
close front document
end tell
end repeat
Thank you for the help
I made several PDF files and download from different sites and I get sample of version 1.3, 1.4, 1.5, 1.7, ..but no 1.6 ! For all of them no issue.
Anyway, because I have not been able to reproduce what you have, I took different approach.
1) I have added at top of the script, a list of encoding/version list which may required special treatment (like hit return key). You can of course amend these 2 lists to manage other cases you may have.
2) I changed your script to allow the script to get encoding and version values of the pdf. This is done by using spotlight data base via shell command 'mdls'. I use it 2 times to get version and encoding characteristics. The shell command returns characters before and after the value we want to get, so I use text x thru Y to extract the encoding and the version itself.
3) if PDF version/encoding are in the list predefined which requires special treatment, then I set OKReturn to true.
4) Just after the save instruction, the script now test if OKReturn is true. then I ask the script to hit return key for you. you may have to adjust this part, for instance, it could be not only 1 return, but 2 or something else. this is something I have not been able to test, because all my pdf are working. please keep in mind that because I simulate return key, you should not use the keyboard during the script run.
based on my test, I don't think the encoding is the blocking criteria. I think the version 1.6 is.Here is the script. It includes comment to make you able to adjust it:
set CodingReturn to {"Mac OS X 10.7.3 Quartz PDFContext"}
set VersionReturn to {"1.6"}
set myFolder to choose folder
tell application "Finder"
set fl to files of myFolder as alias list
end tell
repeat with f in fl
set FVersion to do shell script "mdls -name kMDItemVersion " & quoted form of POSIX path of f
set FEncoding to do shell script "mdls -name kMDItemEncodingApplications " & quoted form of POSIX path of f
if (length of FVersion) > 21 then set FVersion to text 19 thru -2 of FVersion -- extract only version number
if (length of FEncoding) > 42 then set FEncoding to text 38 thru -4 of FEncoding -- extract only the coding
set OKReturn to ((FVersion is in VersionReturn) and (FEncoding is in CodingReturn)) -- special treatment true/false
tell application "Preview"
activate
open f
repeat until (count of windows) > 0
delay 0.3
end repeat
save front document
if OKReturn then -- we need special key to be pressed
tell application "System Events" to keystroke return
end if
close front document
end tell
end repeat
I would be very interested to get your feedback about this version.
To save without the "save as" dialog popping up, add "in f". Here "f" was the filename. You could also put in a different path.
save front document in f

Removing People from Contacts Group (in Mac Address Book)

This is my first post in stackoverflow. I've spent weeks trying to get an Applescript to remove people from established Groups (not Smart Groups) in Contacts (Mac Address Book). The script removes several people then issues an error. If I re-run the script after the error is issued, it will remove a few more people from the group and then issue the same error again. I can continue doing this until eventually everyone is removed from the group. I don't understand why the error is issued when re-running the script after the error is issued results in a few more people being removed before the error is issued again. - And again, I can continue re-running the script until eventually every person is removed from the group. This suggests the contact records are not corrupted.
I've tried moving the SAVE command around but that didn't help. The Group I'm removing contacts from is labeled "Family".
The error issued is...
error "Contacts got an error: Can’t get group \"Family\"." number -1728 from group "Family"
tell application "Contacts"
set group_list to name of every group
repeat with anItem in group_list
set AppleScript's text item delimiters to ""
repeat 1 times
if first item of anItem is not "$" then exit repeat
set AppleScript's text item delimiters to "$"
set gruppe to text item 2 of anItem
if group gruppe exists then
--remove every person from group
repeat with person_to_remove in every person in group gruppe
set firstName to first name of every person in group gruppe
set group_count to count every person in group gruppe
remove person_to_remove from group gruppe
save
end repeat
end if
end repeat
end repeat
save
return "Done"
end tell
I think you're trying to hard. There is no need to change applescripts text item delimiters you can still find out if the group has a $ a the beginning of the group name
creating a 1 time loop is just weird not sure why you chose to do it that way.
you know the group already exists because you are looping through them so no need for that either
so here it is
tell application "Contacts"
set group_list to name of every group
repeat with aGroup in group_list
if first item of aGroup is "$" then
set thePeople to every person in group aGroup
repeat with aPerson in thePeople
remove aPerson from group aGroup
end repeat
end if
end repeat
save
end tell

Simple Apple Automator iterator not working (using AppleScript) - why?

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

Resources