Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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"}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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).

share|improve this answer
    
Aaaaaah! repeat that's it. Thank you. I tried array/list but didn't now how to loop it. My lazy search didn't find it. –  tenshimsm Jun 24 '12 at 0:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.