Customization of safari through applescript

Discussion in 'Mac Programming' started by abidkhan135, Aug 1, 2013.

  1. macrumors newbie

    Joined:
    Apr 1, 2013
    #1
    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.
     
  2. kryten2, Aug 1, 2013
    Last edited: Aug 1, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Example :

    Code:
    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

    Code:
    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 :

    Code:
    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
     
  3. thread starter macrumors newbie

    Joined:
    Apr 1, 2013
  4. thread starter macrumors newbie

    Joined:
    Apr 1, 2013
    #4
    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
     
  5. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Apr 1, 2013
    #6
    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.
     
  7. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    Normally activate should do it in Applescript eg :

    Code:
    activate application "Safari"
    tell application "Safari" to activate
    or you could try this :

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

Share This Page