4
\$\begingroup\$

I got fed up off opening a set of applications when I wanted to code. So I decided to read up on AppleScript and came up with the following task for myself.

Open a application of choice on a given page/directory. Arrange that application depending on your monitor width/height by setting the application display size and position on the screen.

I came up with the following:

global _browserList
set _browserList to {"Safari", "Google Chrome"}
--------------------------------------
on reSizeLoc(_app, _posL, _posT, _appWidth, _appHeight) --Resize and Place application on screen
    tell application "System Events" to tell application process _app
        try
            repeat with x from 1 to (count windows)
                get properties of window x
                set position of window x to {_posL, _posT}
                set size of window x to {_appWidth, _appHeight}
            end repeat
        end try
    end tell
end reSizeLoc
--------------------------------------
on bringToFront(_app) --Bring the application to the front (Focus)
    try
        tell application "System Events"
            tell process _app
                set frontmost to true
                perform action "AXRaise" of (windows whose title is _app)
            end tell
        end tell
    end try
end bringToFront
--------------------------------------
on openDir(_app, _dir) --Open application on a specific page/directory
    if _dir is not "" then
        if application _app is running and _app is not "Finder" then
            tell application _app
                if _browserList contains _app then
                    open location _dir
                else
                    open _dir
                end if
            end tell
        else
            tell application _app
                open alias _dir
            end tell
        end if
    end if
end openDir
--------------------------------------
on openApp(_app, _dir, _posL, _posT, _appWidth, _appHeight) --Main function
    if application _app is running and _app is not "Finder" then
        bringToFront(_app)
        openDir(_app, _dir)
        reSizeLoc(_app, _posL, _posT, _appWidth, _appHeight)
    else
        tell application _app to activate --Bring to front
        delay 0.5
        openDir(_app, _dir)
        delay 0.5
        reSizeLoc(_app, _posL, _posT, _appWidth, _appHeight)
    end if
end openApp
--------------------------------------------------
tell application "Finder" --Get monitor details
    set _dim to bounds of window of desktop
    set _item1 to item 1 of _dim
    set _item2 to item 2 of _dim
    set _width to item 3 of _dim
    set _height to item 4 of _dim
end tell

--Ultrawide
if _item1 = 0 and _item2 = 0 and _width = 3440 and _height = 1440 then
    openApp("Safari", "http://www.google.co.uk", 2292, 0, 1146, 1353)
    openApp("Atom", "Macintosh HD:Users:jjpro:Desktop:Github", 1146, 0, 1146, 1353)
    openApp("Google Chrome", "http://localhost:8888", 0, 0, 1146, 1353)
    openApp("MAMP", "", 0, 0, 400, 200)
    openApp("Finder", "Macintosh HD:Users:jjpro:Desktop:Github", 600, 1, 700, 400)
    return "Complete - Ultrawide mode ONLY"
end if

Just wanted a second pair of eyes to suggest possible improvements? It works perfectly and does exactly what I want it to do; but there is always room for improvement.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.