The script below resizes Safari to full screen (on a MBP 15 inch) and hides the bookmarks bar and the toolbar. If Safari is already full screen, it unhides the two and resizes the window to a smaller size.
tell application "Safari"
activate
if bounds of window 1 is not equal to {0, 22, 1440, 900} then
tell application "System Events"
keystroke "b" using {command down, shift down}
keystroke "|" using command down
end tell
set bounds of window 1 to {0, 22, 1440, 900}
else
delay 0.1
tell application "System Events"
keystroke "b" using {command down, shift down}
keystroke "|" using command down
end tell
set bounds of window 1 to {160, 80, 980, 700}
end if
end tell
The script works fine when run from Script Editor or Quicksilver, or when selecting it from FastScripts (a menu bar utility to launch scripts). But - very strangely - when I assign it a keyboard shortcut (either through FastScripts or Quicksilver), the GUI elements of the script won’t work. It still resizes the window fine, but the toolbar and bookmark bar won’t hide/show.
I was hoping that someone might have some insight into this - situation. Nothing in my searches yielded any. Is it something in my script? Something specific to GUI scripting?
for GUI scripting the process of System Events should be specified
try this
activate application "Safari"
tell application "System Events"
tell process "Safari"
keystroke "b" using {command down, shift down}
keystroke "|" using command down
end tell
end tell
tell application "Safari"
if bounds of window 1 is not equal to {0, 22, 1440, 900} then
set bounds of window 1 to {0, 22, 1440, 900}
else
set bounds of window 1 to {160, 80, 980, 700}
end if
end tell
Thanks Stefan - That’s great advice that I’ll certainly make use of in future scripts.
Strangely, though, the problem persists. Even
activate application "Safari"
tell application "System Events"
tell process "Safari"
keystroke "b" using {command down, shift down}
keystroke "|" using command down
end tell
end tell
won’t run from a keyboard shortcut in either FastScripts or Quicksilver, although it runs perfectly fine from Script Editor, or when explicitly selected in either of the apps. I suppose I’ll try a few more keyboard launchers before I give up - but I can’t see a logical reason for this, so it’s a bit frustrating.
In any case, thanks again for the help! Much appreciated.