PDA

View Full Version : Customization of safari through applescript




abidkhan135
Aug 1, 2013, 02:04 AM
Hi all,
Hope you will be fine. I need help in customization of safari (i.e Hide/Show Toolbar and safari position on MAC window) through apple script. Through apple-script how can i click the "View" menu and then "Show/Hide Toolbar" item. Kindly help.



kryten2
Aug 1, 2013, 02:08 PM
Example :

activate application "Safari"
tell application "System Events"
tell process "Safari"
-- insert GUI Scripting statements here
-- Hide Toolbar
click menu item "Hide Toolbar" of menu 1 of menu bar item "View" of menu bar 1
delay 3
-- Show Toolbar
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1
end tell
end tell


or

activate application "Safari"
tell application "System Events"
tell process "Safari"
-- insert GUI Scripting statements here
-- Hide Toolbar
if value of attribute "AXTitle" of menu item 1 of menu "View" of menu bar item "View" of menu bar 1 contains "Hide Toolbar" then
click menu item "Hide Toolbar" of menu 1 of menu bar item "View" of menu bar 1
else
-- Show Toolbar
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1
end if
end tell
end tell

For position look at the bounds property of a Safari window eg :

tell application "Safari"
-- insert actions here
-- Get bounds of window 1
get bounds of window 1
-- Bounds are {x,y,width,height}
-- Set bounds of window 1
set bounds of window 1 to {0, 22, 1280, 934}
end tell


Note : Tested on Leopard with Safari 5.0.6. UI elements may have changed on Lion,ML or Mavericks. YMMV.

Info: http://pfiddlesoft.com/uibrowser/vs_ui_element_inspector.html

abidkhan135
Aug 2, 2013, 12:09 AM
Thanks. It works...:)

abidkhan135
Aug 2, 2013, 01:09 AM
kryten2:
One more question. I have an app running(Login app) when MAC starts and it covers the whole screen even the top menu bar user cannot use the MAC until he inputs some information and i have placed a button to use Safari and Use case is when user click on that(Safari) button Safari will open on top of my Login app without closing my Login app. How can i force Safari to come to front of my Login app through applescript or objective C. Your previous answer was very helpful and helps me a lot. Thanks

kryten2
Aug 2, 2013, 09:58 AM
Your Login app is written in what language? If it's Objective C then I can't help you with that. Lots of very nice people on this forum with an expert knowledge of Objective C. I'm sure if anyone's interested you'll get an answer to your question. To be quite honest I don't really get your question. You say Safari will open on top of your Login app without closing it. If it does that then doesn't it become the frontmost app? Or if you want your Login app to close after you click that Safari button it's just a matter of putting in some code to do so.

abidkhan135
Aug 2, 2013, 10:22 AM
Yes i want to make Safari as front most app without closing my Loginapp.
There is an option given in my Loginapp to open some specific URL. I want to open that URL in Safari when user click on that option, but i want to make Safari as front most app. hope you get my question. Thanks for your precious time.

kryten2
Aug 2, 2013, 12:54 PM
hope you get my question. Thanks for your precious time.

Normally activate should do it in Applescript eg :

activate
Brings an application to the front, launching it if necessary.

activate application "Safari"
tell application "Safari" to activate

or you could try this :

tell application "System Events" to set frontmost of process "Safari" to true