0

I currently using an AppleScript application that launches a website (phpMyAdmin) in Safari. I keep it in my dock for easy access.

What I am looking for

  • When clicking on the ASA (AppleScript Application) it opens to the safari page
  • When I move to a different window and click the ASA again, it will bring the safari window to the front instead of reopening it in a new window/tab
  • If I quit the ASA the safari page will close
  • If I X out the tab where the website is, the the ASA will quit

If anyone has any idea how to do this it that would be great!

  • Taking into consideration all four bullet points, I do not believe that's reasonably doable with simple basic normal AppleScript, you'd probably have to get into AppleScriptObjC. IMO It would be much easier to focus on just the first two bullet points. I'd write it so when the app is run, it checks to see if the target tab exists and if not then opens it, otherwise bring it to the front and quit. I really do not see a need for the last two bullet points, just close the page when you're done with it and the app is already closed anyway. – user3439894 Aug 15 '18 at 16:13
  • @user3439894 Thanks for info. I agree I would do it the way suggested. The last two bullets are incase someone uses keep run handler open. – JBis Aug 15 '18 at 16:15
  • IMO There is no need to keep alive after the run handler, and if you keep it open then code has to loop and that's just not practical to achieve a reasonable working solution focusing on just the first two bullet points. – user3439894 Aug 15 '18 at 16:21
  • Would you be interested in a solution that puts phpMyAdmin into its own “app”? If so, something like fluidapp.com seems like the ideal choice. – TJ Luoma Aug 15 '18 at 19:23
  • @TJLuoma Thats based off of Electron I think. Maybe good idea. – JBis Aug 15 '18 at 19:44
1

As other users have noted, the third and fourth bullet points are probably not worth holding on to, as it makes the first two difficult to implement:

use S : application "Safari"
property PHPMyAdminURL : "http://xxxxx/ppmya"


on run
    if S is not running then return openPHPMyAdmin()

    set _T to a reference to (every tab of every window of S whose ¬
        name contains "PHPMyAdmin" and ¬
        URL contains "/ppmya")

    if (count _T) > 0 then return viewPHPMyAdmin(_T)

    openPHPMyAdmin()
end run


to openPHPMyAdmin()
    tell S
        if (count windows) = 0 then
            make new document with properties ¬
                {URL:PHPMyAdminURL}
        else
            make new tab at end of tabs of S's window 1 ¬
                with properties ¬
                {URL:PHPMyAdminURL}
            set current tab of S's window 1 to the result
        end if

        activate
    end tell
end openPHPMyAdmin


to viewPHPMyAdmin(T)
    set [T] to T
    set W to the first window of S whose tabs contains T

    set current tab of W to T
    activate S
    set the index of W to 1
end viewPHPMyAdmin

After some initial feedback from @user3439894, I removed the on quit handler as trying to use the script as a stay-open application prevents item 2 from being effectively implemented.

  • 1
    I have not tested your code however, at first glance this is written to stay open after the run handler in order to satisfy the third bullet point, right? Well I fail to see how this will handle the second bullet point after the app is first run and stays open. Clicking the open AppleScript App's Dock Tile is not going to run any additional code to set focus to the target tab again. – user3439894 Aug 16 '18 at 3:33
  • @user3439894 Yes you are right. I should have read your initial comments against the OP's question more finely, as that's exactly the warning you gave from the outset. – CJK Aug 16 '18 at 3:46
  • Good script. Gonna see if I can find a way to fix issue with the on quit handler. Thx. – JBis Aug 16 '18 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .