Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript: How to reload a Safari window without bring the window to the foreground while I am on other Desktop space

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

Posted on Jan 4, 2014 8:59 AM

Reply
Question marked as Best reply

Posted on Jan 6, 2014 3:45 AM

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.

7 replies
Question marked as Best reply

Jan 6, 2014 3:45 AM in response to Hiroto

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.

Jan 4, 2014 10:58 AM in response to Mr. Latte

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?

Jan 4, 2014 11:42 AM in response to Mr. Latte

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.

Jan 4, 2014 11:59 AM in response to VikingOSX

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

AppleScript: How to reload a Safari window without bring the window to the foreground while I am on other Desktop space

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.