I need to autoincrement a variable in a loop for Automator.
I have no idea to do it... I'm very new in AppleScript, have been looking around and googling but didn't find much info.
Any ideas ?
You can use something like this:
set myVar to 10
repeat with i from 1 to 5
set newVar to myVar + i
display dialog (newVar)
end repeat
Did this help?
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.
So I want to have different Variables with the same name, except for the numbers at the end. The number increases over time, since it's in a repeat loop.
set i to 0
repeat 6 times
set i to i + 1
set SampleVar & i to i + 10
end repeat
end run
This Syntax is probably completely wrong but, I hope you understand, what I want to achieve.
As already mentioned in the comments by red_menace:
You cannot dynamically create variables like that, they need to be declared at compile time. The normal way to use collections would be to use a list or record.
This is your script rewritten to use lists:
set SampleList to {}
set i to 0
repeat 6 times
set i to i + 1
set the end of SampleList to i
end repeat
I don't know what the advantages of writing it as a record are but it usually takes longer and I'm not quite as familiar with them as I am with lists.
I'm using QTP 11.0 which uses VBScript for it's "language".
I have the following four lines of code:
For x = 1 to 8
msgbox(x)
update1 = SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(x,2)
Next
The return values are:
1
3
3
3
3
3
... forever
I can not seem to use the counter variable in a vbscript for loop without it somehow becoming corrupted - no matter what the name of the variable. I have even tried assigning X to another variable and using that in my GetValue statement with no success.
Am I missing something really simple here? No documentation on Google for a vbscript For loop implies any different usage. I found nothing in reference to QTP either.
Thanks,
Jason
This is very strange, I assume that if you remove the second line in the loop and just leave the MsgBox then x is incremented correctly.
Perhaps ActiveSheet.GetValue takes the first parameter by reference and modifies it. You say that you tried using a temporary variable and it didn't work, have you tried something like this?
For x = 1 to 8
tmp = x
update1 = SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(tmp, 2)
MsgBox("x=" & x & " tmp=" & tmp)
Next
If the problem is that GetValue indeed changes the index you can try using a function that takes the index by value and then use that in the loop.
Public Function GetVal(ByVal index)
GetVal = SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(index, 2)
End Function
Suppose my script contains a variable with the value "foo". How can I create a record with the value {foo: 3} without knowing in advance that the value might be "foo"?
I would think that there is another approach to your problem (especially if you are using ASObjC), but you can do this by using run script, for example:
set myLabel to text returned of (display dialog "Enter record label (key):" default answer "foo")
set myRecord to run script "return {|" & myLabel & "|:3}"
myRecord --> {foo:3} (using the default answer)
Edit: I surrounded the label (key) with pipes in case a reserved term is used.
Another approach is to use Late Night Software's "List & Records Tools" free osax which provides the needed capability to get and set user properties.
http://www.latenightsw.com/freeware/list-record-tools/
Set myRecord to myRecord & (set user property foo in myRecord to 3)
I am aware that this is a long answered question but it was one of the first that I found whilst researching this and attempting it myself. I also wanted the variable I was setting (Not the key) to be determined at runtime which I accomplished like so:
set scr to "on run argList
return {|" & dictKey & "|: (item 1 of argList)}
end run"
set newDict to (run script scr with parameters {dictVal})
Where dictKey is the variable to use as the key for the record and dictVal is the value to set it to. Hope this helps!
I have some buttons that get disabled when adding a user.
What I need to be able to do is have firewatir wait_until something is present.
I am trying to use this right now:
count = 10
while count > 0
browser.button(:name, "_eventId_addEmployee").click
Watir::Wait.wait_until_present {text.exist? newHireUsername}
end
count -= 1
end
For some reason I can't get the wait_until method to work correctly.
Thanks in advance!
I usually do something like this,
browser.element_type(:id, "xxx").wait_until_present
Instead of using the wait_until_present option I used an until loop to wait for text to appear.
This by-passed attempting to use it so it does not qualify as an answer for my original question but want it here for others.