Applescript array of applications
I have a script that reload the active tab of an open browser, but I want it to do with all opened browsers. Is there a way to make a list/array and use it in this code?
This code works with one browser only:
if application "Safari" is running then
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
keystroke "r" using {command down}
end tell
end tell
end if
I've tried to create an array/list but I think this is no the way because it doesn't work:
set browsers to {"Google Chrome", "Firefox", "Opera", "Safari"}
You generally can't substitute a list for a string and have things work without some other changes.
A good way to get around this is to use a repeat with variable in list
approach. This lets you go through each item in a list and run your code with each item in the list individually.
Here's how to apply that to your code:
set browsers to {"Google Chrome", "Firefox", "Opera", "Safari"}
repeat with browser in browsers
if application browser is running then
tell application browser
activate
end tell
tell application "System Events"
tell process browser
keystroke "r" using {command down}
end tell
end tell
end if
end repeat
Everything is the same except that your code is wrapped in a repeat with
block and the "Safari"
literal is replaced with browser
, a reference to the current browser in the list of browsers (the code will be run once for each browser in the list).