Run AppleScript for Specific Monitor


I've enabled Dictation for command prompts via voice [got it from the OS shipped Chess application in Lion]. Decided to give it a go System Wide now in 2015 running Yosemite. I fancy it so far, certainly looks better and performs better as opposed to Lion (which should be the case) There are a few customizable options when you 'Enable Advanced Commands' like open file, run workflow, open application, paste text, paste data, & keyboard shortcut.

However, call me lazy but it's still something that matters to me, I frequent Netflix & Hulu and other websites at minimum 4 times a day. I would like to add a functional command that will automatically open a webpage correlating to that command. since open file is different than run file, i'm trying to autonomously open Safari + the name of the website in URL form with dictation? The good thing about this is it automatically makes whatever application you reference the front-most window. So coding should be minimal, and I really don't want to create a service for this [my services are growing, currently at +55 in total].

I am an Automator aficionado however, and well versed with it so I'm using the Dictation Command tool, but what i am not, is an AppleScript scholar. After reading through the applescript library for about 60 minutes i finally figured out how to open a new window of Safari, activate the application in case it was not open and launch the url:

tell application "Safari" to activate
    tell application "Safari"
        make new document with properties {URL:""}
end tell

Additionally, I have 3 monitors. I always have movies, tv etc played on Number 2! I tried putting Safari in the monitor 2 screen with applescript but as an iOS developer, in good conscience am too embarrassed to share my code, it's sloppy . Can anyone lead me in the right direction? Or does anyone know a more simplified way around all of this? I really want this to work. I am in the military so our rooms aren't that big so my 2nd monitor (27") is my tv, so I have created an iPhone app for my needs only that strictly simulates the double fn press needed to activate Dictation so I can watch tv from my bed [don't have apple tv].

EDIT: My main goal is to be able to use voice activation to populate a Safari specific website using Dictation for a specific monitor. I don't want to sacrifice my 'Spaces' or 'Mission Control' settings as I have done a lot of work to make those beneficial to my workflow. I have 3 monitors, which are set up in succession (i.e, monitor 1 & 3 are right next to one another on my desk station, monitor 1 is left & 3 is right, monitor 2 is no where near 1 or 3 it is facing my bed see pic 1 for reference. However, they are set up in succession 1 goes to 2, 2 goes to 3. I have spaces set up where specific apps use monitor number 3 exclusively (Xcode) which doesn't make it as much of a hassle as it sounds, this is why i don't want to sacrifice my spaces settings because I use 2 as my dedicated "TV", this isn't that big of a deal though and can be changed, I don't even know why I do this, but it works so I haven't really bothered to try anything else)

PIC 1:

miken32's suggestion works, but only to display it, not to minimize. Minimizing is important, because I will have to minimize the active Safari website, lets say Hulu, just to open Netflix because full-screen apps can't overlap. So I will inevitably do another voice activation or otherwise to minimize so I can transfer websites without blimps. Note: sometimes I don't want to close, sometimes I save my spot so minimizing is a good resource for me. This is what happens with miken32: it minimizes but I think it's trying to go back the origins we set in the script, but negatively as in (-). NOTE: I know the origins you see in the applescript of the movie don't reflect the absolute origin for my specific monitor, I changed the origins for testing, but for recording the movie I had to put it on that screen so I kept it as is just so you can see where it goes when you minimize. Other than that works great for the amount of code!. I'm glad you guys included the full screen option even when I didn't ask for it, that's absolutely what I had in mind but didn't want to disturb anyone trying to figure that out, my main goal was just getting it on the 2nd monitor.

markhuntes answer works flawlessly, the amount of time it took to load on the first run was about 15 seconds, nothing to worry about, but definetly not optimal for every day use, however, subsequent runs were minimal (3-6 seconds max). What I do like about his suggestion is that it automatically finds the second monitor instead of me having to hard code it, which is great from a consumer point of view, the less I have to tamper with the better, this is a great feature for the future, say I change my set-up or I get a different sized monitor etc I don't have to re-code an absolute origin. There is a drawback as of now, I'm going to play around with it for a couple of days just to make sure its not conflicting with my other system preferences. It minimizes where it should which is great, it populates accurately but i've found if Safari is already the front-most application it (sometimes, more precisely, rarely, it populates in the same Space or (monitor 1) that Safari is already active. The reason I wanted it to show specifically in Monitor 2 is because of course I don't just use Safari in that monitor alone. Again, it could be my settings, I have a lot of hot corners and mission control keys and spaces preferences set up so I'm going to tinker with it just to make sure its not me and get back to you

Best Answer

  • This script in an Automator Dictation command seems to work.

    Not the fullscreen will not work properly if run in Automator or Script editor. It picks up the previous window 1.

    But any time I got the Dictation command to work. ( always find a bit of a pain to get them to understand me) It got the right window 100%.

    This script uses system profiler to get the monitors. and a bit of simple jiggery to use the width of monitor 1 to determine where bounds are of monitor two.

    I did it this way because you may choose not to use fullscreen

    I have also tried to make the script accessible to people with one monitor

    set theScreens to paragraphs of (do shell script "system_profiler SPDisplaysDataType -detailLevel |grep \"Resolution:\" | awk -F\"Resolution: \" '{print $2}'")
    set theMonitor to ""
    set the counter to count of theScreens --- set to 1 if you want to test it on monitor 1
    if (counter) > 1 then
        -- we will do things for monitor 2
        set thisDisplayWidthHeightMonitor_2 to {(word 1 of item 1 of theScreens) + 2, 0, ((word 1 of item 1 of theScreens) + (word 1 of item 2 of theScreens)), word 3 of item 2 of theScreens}
        set theMonitor to thisDisplayWidthHeightMonitor_2
        -- we will do things for monitor 1
        set thisDisplayWidthHeightMonitor_1 to {0, 0, (word 1 of item 1 of theScreens), (word 3 of item 1 of theScreens)}
        set theMonitor to thisDisplayWidthHeightMonitor_1
    end if
    tell application "Safari"
        make new document with properties {URL:"", index:1}
        set bounds of window 1 to theMonitor
    end tell
    tell application "System Events"
        tell application process "Safari"
            set value of attribute "AXFullScreen" of window 1 to true
        end tell
    end tell