Hi,
I have an AS applet “Restart or Shut Down” kept in the dock to restart or shut down after quitting any open apps other than the finder and itself. If an open app needs saving, it asks to save or not. Written in ML, it works now in Mavericks as well but, with one very annoying quirk.
My problem is that I have two monitors and I want the applet dialog box to open always on the main display (Display 1), regardless if I happen to have the focus on the second display. For this, while spaces were already enabled in Mission Control, I right-clicked on the applet in the dock, navigated to Options > Assign To and ticked “Desktop on Display 1”.
That doesn’t work in Mavericks as advertised :mad: - the applet dialog opens always on the display I happen to be focused.
This is the start of my script:
activate
display dialog "Quit any open apps and Restart or Shut Down" with icon stop ¬
buttons {"Cancel", "Restart", "Shut Down"} default button 1 giving up after 30
set DialogResult to result
---
Please, what can I do to force the applet to open always on Display 1, either in the system setup or in the script?
Another option, that may be easier, is to move the front window of some app, like finder to a positon within the boundaries of the first monitor.
There is an article over at DaringFireball.net that describes monitor setup and coordinates pretty good. I think the search term would be “AppleScript bounds desktop window”.
I have been “teleporting” windows for years no, and there are a couple of keys to success:
it is easier when you have the same resolution on both monitors.
Once you have a setup, that is, if the secondary display is to the left or right, stick with it.
Hi McUsrII,
Obviously, if I click anywhere on the main display, the focus will change to that display and then i can click on my applet in the dock. But, I don’t want to have to remember to change the focus manually. In Lion and ML that wasn’t necessary.
In Mavericks, you are supposed to control where an app should start by assigning the app to a display. However, there must be a bug somewhere because it works only for some of the apps and inconsistently or not at all for others.
Is there any way to script the change the focus and open the dialog on a specific display?
Note: I corrected my display dialog code at #1. Somehow I missed the dialog text
Edit:
Sorry, been a little slow in replying to your first post and missed your second.
I’ll have a look at that possible solution.
I checked that. It only explains and shows how to get the bounds of windows on multiple displays. In my case I know the bounds and I’d just like to place the dialog box at say {500, 500}. But, specifying a custom position of a dialog box is apparently not possible in AS, according to StefanK at #2 in http://macscripter.net/viewtopic.php?id=37591 (unless using a hack Nigel Garvey shows but not recommends at #3 there)
So, I’m back at asking how to script the change of focus to the desired display and then open the dialog box.
Is there any clever solution out there
How about using system events to find the first window that is on your screen 1, move it a pixel to the left, then a pixel to the right, and then show your dialog?
The fallback, if there are no windows, is to select an icon on the Desktop, that you are dead sure will be there.
If you don’t have any ideas about an icon that is always there, then I suggest a home made trash can.
This isn’t very elegant but it works for me after I have granted SystemUIServer rights to control my machine.
You’ll have to adjust it so it suits your own geomtery.
And if it doesn’t, then you better use do shell script open on the bundleidentifier of the foreground process after a short delay, so that it executes after the script has finished. Having said that, this works for me when I execute the script from the menu of the secondary display.
tell application "System Events"
tell first window of (first application process whose frontmost is true and visible is true)
set pos to its position
set its position to {0, (item 2 of pos)}
end tell
end tell
I like a pristine desktop so, no home made trash can
But the startup disk is there. This is an ugly hack with a flashing window (at least it wakes me up) but it does the job of changing the focus to the main display:
tell application "Finder"
activate
select item 1 of (list disks)
close window 1
display dialog "Quit any open apps and Restart or Shut Down" with icon stop ¬
buttons {"Cancel", "Restart", "Shut Down"} default button 1 giving up after 60
set DialogResult to result
end tell
-- the rest of the script
Can somebody improve on this, make it less of a hack, maybe something able to change focus to any display?
Regarding your suggestion at #7, if I understand it correctly, it assumes that there is an open app there. I may just have various finder windows on that display.
Yes that was the idea. You’d have to improve on it to be really robust. As long as it is an open app somwhere with an open window it should work, with as litle intruision as possible.
Edit
My code-snippet was just a proof of concept, in order to use it, you’d rather see if the front window is on the main display before moving it around, and you’d also check to see if there were any windows open, and if it weren’t then you’d select an icon on the desktop.
Not sure if McUsr mentioned this already, but does the second display show in Mission Control? If it is then you can set the shortcut (^1, ^2, … ) for the second display in the Keyboard preferences.
Hi kel,
No, he hasn’t, but I do know about the shortcuts. My points are:
I know how to switch focus manually, either by shortcuts or clicking anywhere on the desired display, but I don’t want to have to be always aware where the focus is, or to always remember the display I assigned an app to.
I really want the advertised Mavericks feature, by which you only have to assign an app to the desired display. It seems a clear bug in Mavericks since it doesn’t work that way for many apps. Hopefully, Apple will fix it one day.
In the mean time, I was looking for a scriptable workaround. With McUsr’s suggestions I have now two possible solutions, his at #7 (when there is at least one app window) and mine at #8 (when there are no app windows). Combining the two ideas, I’ll cover any circumstances on the main display.
I’m still open for any better scriptable solution, especially one for switching to the second display. I still can’t really believe that Apple hasn’t provided a scriptable solution for switching focus between displays.
Well, if they didn’t provide an opportunity directly, you must say that they provided one intrisically, you were able to solve your problem weren’t you?
I believe this is what scripting is about, because we are really a number of individuals out here, with individual needs.
I’ll give you right in that sometimes it is confusing which window an app pops up in, but I’ll give Apple some slack in that direction.
The way Maverick handles screen handling is a giant leap forward, at least for me who time-warped from Snow Leopard.
Isn’t the second display in the last space? If so, this looks like it’ll work if you have less than 20 spaces besides the second display:
tell application "System Events"
try
repeat 20 times
-- 23 is "5"
-- 124 is right arrow
key code 124 using control down
end repeat
on error err_msg
display dialog err_msg
end try
end tell
delay 1
do shell script "osascript -e 'display dialog \"It works!\"'"
note that you need to tick the check boxes for the shortcuts.
Hi McUsrII,
I can really understand how happy you are. I remember years ago when I switched from G3 to Intel iMacs. Wow!!!
Now, I was probably spoiled by Lion and especially ML, where the apps were simply remembering the display they last started on. Anyway, many thanks for your suggestions leading to the combined workaround.
Hi kel,
I’m away from my Mac, so I can’t yet test this. Can you please explain what "keystroke “5"” is supposed to do? Is it equivalent to ^1 and if so, what is the keystroke for ^2 ?
This is one of the areas where I am not totally confident yet, I haven’t quite grasped the rules for this yet.
But mission control and the two displays that operates individually, together with old Exposè functionality, makes my days very pleasant indeed, this and the new bookmark solution for Safari!
Hi kel,
I seem to be too slow for your quick posts
I only have 4 spaces (1 on display 1 and 3 on display 2) plus another one more coming on display 2.
Can you please explain what that code does, as i’m away from my Mac?
I have it working now. This will switch to desktop 1 (^1), display a dialog, and return to desktop 5 (^5):
-- switch to desktop 1 with ^1
tell application "System Events"
key code 18 using control down
end tell
delay 2
-- display the dialog in desktop 1
tell application "Finder"
activate
ignoring application responses
display dialog "It works!"
end ignoring
end tell
delay 5
-- go back to desktop 5 with ^5
tell application "System Events"
key code 23 using control down
end tell
‘key code 23’ is the text for “5”. The control characters ^1 switches to desktop 1, ^2 switches to desktop 2, … I think you need the delays, but 5 seconds might not be necessary. Remember that you must check the checkboxes in System Preferences > Keyboard > Shortcuts for Mission Control.
Still in testing.
Hope this example explains it.
Edited: I’ve found that after it goes back to desktop 5 and the script ends, when you switch to desktop 1 the Finder is not frontmost anymore. It might be a good idea to leave 1 desktop on your main display blank. So that when you switch to that desktop, you can see the dialog.
Edited: btw, here’s the key codes for digits 0 thru 9 in hexadecimal (0x12 hex is 18 decimal or the character “1”):
It’s on your computer, but I got this from the internet.
Hi kel,
Sorry for being so late but I had one of those crazy days.
I like your idea very much because in principle it allows scripted switching to any space on any display. However, as is, it doesn’t work consistently in all circumstances (at least for me):
If there are two spaces Desktop 1 and 2 on Display 1, and the Desktop 2 is visible but not in focus (the focus being on one of the spaces on Display 2 and the script is run from there), then the script does always switch to Desktop 1 on Display 1, and most of the times the dialog box does pop up on the newly reviled Desktop 1. The dialog box fails occasionally to pop up on Desktop 1, displaying instead in the space where I run the script. I can’t work out why this happens, but it seems to have something to do with the timing and focus when resetting the test from Desktop 1 to 2.
Pointing to a possible solution is the fact that this intermitted occurrences can be avoided by clicking quickly anywhere on the newly reviled Desktop 1 within the 2 seconds delay before the dialog is to be displayed.
If there is only one space i.e. only Desktop 1 on the main Display 1 (my normal setup), then the script as is does always fail to display the dialog on Desktop 1. The dialog is always displayed in the space the script was run from.
Again pointing to a possible solution is the fact that this can be avoided by clicking quickly anywhere on the out of focus Desktop 1 within the 2 seconds delay before the dialog is to be displayed.
As you probably have already guessed, the possible solution I’m hinting at, is to get System Events to click anywhere within the space it has just switched to, so as to fully focus on that space, before attempting to display any dialog or do any work there. Note that it doesn’t seem to matter where you click in that space (desktop, app or finder window). I think that this will also fix your finding about the Finder on Desktop 1 not being frontmost when switching back to that space.
Note that I added “giving up after 3” to the dialog to allow full focus on return to Desktop 5 (Too lazy to dismiss the dialog quickly).
Many thanks for your help,
Chris
Hi McUsrII,
Many thanks for the Teleport. I’m also a fan of optimistic science fiction.
Chris
If display notification doesn’t show in display 2 then you might use that:
-- switch to desktop 1 with ^1
tell application "System Events"
key code 18 using control down
end tell
delay 0.2
-- display the dialog in desktop 1
display notification "hello"
delay 0.2
-- go back to desktop 5 with ^5
tell application "System Events"
key code 23 using control down
end tell