I use my Mac Mini to play movies and music on while working on my MBP. I like to keep screen sharing open but reduced in size in the upper right corner of my screen so that I can effectively have a "PIP". When I want to tag a song that is playing or change the movie, I use a key command to make the window full size, then another key command to shrink it back and position it in the upper right corner.
Since I updated to Lion, I've been getting the following error:
error "Screen Sharing got an error: Can’t set window 1 to {1191, 22,
1441, 185}." number -10006 from window 1
The code is below. Does anyone know what I'm doing wrong?
tell application "Screen Sharing"
activate
set the bounds of the first window to {1191, 22, 1441, 185}
end tell
I have also tried changing the code to use the wording "set the bounds of window 1..." but get the same error.
Any help would be much appreciated.
--Adam
If you look at the applescript dictionary for the application, it doesn't know those commands (e.g.. window or bounds). It only knows the GetURL command so it certainly won't work.
However there is another option. System Events knows about windows and it knows the size and position commands. So you can use that instead...
tell application "System Events"
set ssProcess to first process whose name is "Screen Sharing"
tell ssProcess
tell first window
set position to {0, 20}
set size to {605, 400}
end tell
end tell
end tell
tell application "Screen Sharing" to activate
Related
I record a lot of videos, so I have a MacBook Pro with two monitors connected. One of those monitors has a resolution set to 1920x1080. I record all my videos from that specific monitor. Right now, I run Google Chrome and Visual Studio Code. Then, I resize the windows for the specific conditions of my recording (sometimes Chrome uses more space, sometimes VSC does), so this is not a matter of simply doing a side-by-side setup where each app takes up a certain percentage.
That manual process is tedious and I usually want them to be a very specific size based on what I am recording. That is why I want to use AppleScript to simply resize the currently running applications based on various recording scenarios.
To create a test case, I wrote the following code. While it does the trick of perfectly resizing the app, it moves Chrome from the monitor I am recording on to my MacBook Pro's connected display.
Here is what I wrote so far:
set theApp to "Google Chrome"
set xLocation to 0
set yLocation to 0
set appWidth to 750
set appHeight to 1080
tell application "Google Chrome"
set bounds of front window to {0, 0, appWidth, appHeight}
end tell
Unfortunately, I am very new to AppleScript and have not been able to figure out how to prevent the script from moving the app (Google Chrome in this case) from the current monitor to the MacBook Pro monitor. I suspect it is that front window code, but I am struggling to learn how that works as I cannot find the documentation.
How can I use AppleScript to resize an application that is currently running without moving the application's window to another monitor?
I suppose if the answer is to open a new copy of the application, that would work too, so long as I can open or move it to the proper monitor. Thanks!
Update
Close, but not quite there...
I was provided this link in the comments. The code looks like this:
set theScreens to paragraphs of (do shell script "system_profiler SPDisplaysDataType -detailLevel |grep \"Resolution:\" | awk -F\"Resolution: \" '{print $2}'")
set theMonitor to ""
log theScreens
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
else
-- 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"
activate
make new document with properties {URL:"http://netflix.com"}
set bounds of window 1 to theMonitor
log 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
If I log the variable theScreens, I get the following result:
(*3072 x 1920 Retina, 3840 x 2160 (2160p/4K UHD 1 - Ultra High Definition), 4608 x 2592*)
That code does not show me the correct x position I need for my window placement. It just shows the monitors I am using and their UHD resolution.
When the code opens Safari (in this case), it sets the bounds with the variable theMonitor, and it does indeed open in the monitor I want it to. The output is:
3074, 0, 6912, 2160
Since the code does display the application in the monitor I want, I assume that 3074 is the correct x position for my need, so here is my updated code:
tell application "Google Chrome"
set bounds of front window to {3074, 0, 750, 1080}
end tell
Unfortunately, Google Chrome completely disappears off the screen and is nowhere to be found on any monitors or desktops. I have to quit Chrome and restart so I can use it again (or do the change the change-the-screen-resolution trick).
I am still left with the following question: How do I open the app in the correct monitor I desire and in the x & y location I desire?
I'm using an applescript within automator to start a quicktime recording. When I run the applescript from automator, everything works fine, but when I run the same automator sequence as a double-clickable application, it throws an error* when it gets to this line:
tell application "System Events" to tell process "QuickTime Player" to set position of front window to {1131, 725}
Any ideas as to why?
Thanks!!
*The error is just, "The action “Run AppleScript” encountered an error"
You might be getting the error because your application doesn’t have permission to access GUI Scripting.
However, you can position and resize windows in QuickTime Player without GUI scripting like this:
tell application "QuickTime Player"
tell window 1
set the bounds to {100, 100, 640, 480}
end tell
end tell
The 4 numbers that set the bounds are the x position, y position, window width, window height. So the above script tells the first window to position itself 100 pixels away from the top of the screen, 100 pixels away from the left of the screen, and to be 640 pixels wide and 480 pixels tall.
Look in QuickTime Player’s AppleScript dictionary for the commands that enable you to start a recording with AppleScript. Open the Script Editor app and choose File > Open Dictionary and choose QuickTime Player.
I am currently using applescript to setup various parameters of the desktops at my university. So far my script successfully changes the desktop background and the dock size.
The problem at hand is when I run the script, majority of the time, the icons on the desktop never change.
Here is the script I wrote to alter the desktop icon's size and grid spacing:
tell application "System Events"
set finderPrefsFile to property list file "~/Library/Preferences/com.apple.Finder.plist"
tell finderPrefsFile
tell property list item "DesktopViewSettings"
tell property list item "IconViewSettings"
set value of property list item "gridSpacing" to "100"
set value of property list item "iconSize" to "32"
set value of property list item "arrangeBy" to "none"
end tell
end tell
end tell
end tell
#Restart Finder for changes to take effect.
do shell script "killall Finder"
How should I go about altering the script to make it work all the time (I would like to eventually share this script with some of my classmates).
P.s.
Here is the full script
I didn't get your script work reliable. I played around with timeouts but didn't get the Finder to refresh using the new settings.
But I found to set some view options directly using vanilla AppleScript:
tell application "Finder"
activate
set iconViewOptions to desktop's window's icon view options
tell iconViewOptions
set arrangement to not arranged
set icon size to 32
set shows item info to false
set shows icon preview to false
end tell
quit
end tell
delay 1
tell application "Finder" to activate
The AppleScript quit-handler works more reliable then do shell script "killall Finder", maybe the killall is too hard...
The delay 1 does the magic to give the Finder the time to breath before get up again and using it the Script works each time...
But one thing is AFAIK not possible in Finder scripting: Setting the grid space :-/
Greetings, Michael / Hamburg
I am trying to get the foreground application from my second monitor (In Mavericks, second desktop). Here's my code that only gets the foreground application:
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
How can I change it so it gets the application from a specific desktop / screen?
I don't think you can do as you want. If you look at the properties of frontApp there is no property that would indicate which screen it is on. However, what you can do is check the location of the application's windows. If you get the properties of a window of a process then you will see that it has a "position" property. You could check those coordinates to determine which screen it is on.
For example, I have 2 screens. My laptop is setup to be the main screen. I know the screen resolution of the main screen is 1680x1050. Therefore, if I check a window and it has a position outside of those coordinates then I know it must be on the second monitor. Here's how I could do that.
NOTE: I can tell which applications have windows on the second monitor, but not which application on the second monitor is frontmost... like you asked. You'll have to figure something else out for that. I'm showing you this as an idea that maybe you can make workable for your situation.
Here I am only getting the first application with a window on the second monitor. This should show you the idea and you can adjust the code to do as you need.
set mainScreenResX to 1680
set mainScreenResY to 1050
tell application "System Events"
set firstFoundAppOnSecondScreen to missing value
set visibleApps to application processes whose visible is true
repeat with visibleApp in visibleApps
try
tell visibleApp
set {x, y} to position of window 1
if x > mainScreenResX or x < 0 or y > mainScreenResY or y < 0 then
set firstFoundAppOnSecondScreen to name
exit repeat
end if
end tell
end try
end repeat
return firstFoundAppOnSecondScreen
end tell
How can I set focus to a specific window of a given application using applescript?
I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.
I need two things, one script that collects the window ID's and prints them to stdout. I've got this:
tell application "iTerm"
set wins to id of every window
end tell
which prints 6 integers: 3034, 2528, -1, -1, -1, -1
Bonus Question: What are the four -1's ?
Then I try:
tell application "System Events"
activate window 3034
end tell
Upon which the only thing happening is that I lose focus of my current terminal (in which I am typing these commands), not matter whether I specify 3034 or 2528 as the ID.
You almost have it. You can filter out the "-1" window IDs as by only looking at visible windows:
tell application "iTerm 2"
set wins to id of every window whose visible is true
end tell
I figured this out by looking at the results of:
tell application "iTerm 2" to properties of every window
I noticed that the "-1" windows have the property visible:false
Then you can tell the window ID directly to the iTerm application instead of system events:
tell application "iTerm 2"
activate window 13195
end tell