7 Replies Latest reply: Jan 6, 2014 3:45 AM by Mr. Latte
Mr. Latte Level 1 (5 points)

I want to write an AppleScript to achive a simple task. That is, I open a Safari window on Desktop 1 and automatically reload every 30 minutes while I do my dialy work on desktop 2.

 

Here is my script:

 

tell application "Safari"

    activate

    tell application "System Events"

        tell process "Safari"

            keystroke "r" using {command down}

        end tell

    end tell

end tell

 

This script works. However, whenever the script is executed, my current Desktop 2 will be brought back to Desktop 1. It is distracting to my workflow.

 

Is there any way to just let Safari to reload in the background without bring Safari window to foreground on Desktop 1?

 

I have done a coumple of researches; many of them say I should not use "activate" in my script. I tried that but then the script will just do nothing.


Mac OS X (10.7.3), iMac, MacBook Pro, Mac Pro, iPhone, iPad, and more
  • VikingOSX Level 6 (15,873 points)

    In Mission Control Preferences, what happens if you uncheck Automatically rearrange Spaces based on most recent use. You will need to cycle login to set this. Does this leave Safari on Desktop 1, when you run your refresh?

  • Mr. Latte Level 1 (5 points)

    @VikingOSX,

    After uncheckinging "Automatically rearrange Spaces based on most recent use" option, the script is still not doing what I want to achieve.

     

    Can you be more specific on what is cycle login exactly? I have only 1 account on my own MBP machine.

     

    Thanks.

  • Mr. Latte Level 1 (5 points)

    Say, I am on Desktop 2. The following script somewhat achieves what I want to do but it is not so elegant:

     

    tell application "Safari"

        activate

        tell application "System Events"

            tell process "Safari"

                keystroke "r" using {command down}

                delay 1

                key code 124 using {control down}

            end tell

        end tell

    end tell

     

    How can I just let Safari to reload by itself on Desktop 1 and in the background without Desktop space being changed?

  • VikingOSX Level 6 (15,873 points)

    Cycle login means you need to logout and back in to your account. This is the criterion for enabling changes within Mission Control Preferences.

     

    Have you explicitly assigned Safari to open only in Work Space 1? In OS X Lion, with Safari open in Work Space 1, right-click the Safari Dock icon, and under options, choose Assign to this Desktop.

     

    Once you get this working correctly, you may want to update your delay 1 to delay (1800) which is every 30 minutes.

  • Mr. Latte Level 1 (5 points)

    @VikingOSX,

    Assign Safari to Desktop 1 won't help. My goal is that I want to stay in Desktop 2 all the time such that when the AppleScript is executed, I would not be bothered to shift back to Desktop 1 and then back to Desktop 2.

     

    Imagine that it does the shifting of the Desktop back and forth every 30 minutes, it would be very annoying throughout the day.

     

    Thanks.

  • Hiroto Level 5 (6,744 points)

    Hello

     

    JavaScript would be your friend.

     

    tell application "Safari"
        tell window 1
            do JavaScript "window.location.reload(true)" in current tab
        end tell
    end tell
    

     

    Regards,

    H

  • Mr. Latte Level 1 (5 points)

    Hi, Hiroto

     

    your approach works perfectly fine. Thank you.

     

    I just found there is also a more generic approach without tweaking of javascript or any third party scripting language involed:

     

     

    #!/usr/bin/osascript

     

    tell application "Safari"

        set docUrl to URL of document 1

        set URL of document 1 to docUrl

    end tell

     

     

    From there, I can do a simple cron job task now.