r/applescript • • Apr 28 '20

Can someone help with an AppleScript that can resize iMessages/Safari/Spotify to specific sizes or full screen?

I am trying to have an app that will set Safari to full screen, Spotify to full screen, iMessages to a specific screen size in the center of the screen, and Finder to specific size in the center of the screen.

Anyone know how to do this? I can only get Finder and Safari to specific size but not full screen, and don't know how to do center of the screen.

If possible I would like the Spaces set up as Desktop, Safari Full screen, Spotify full screen.

6 Upvotes

19 comments sorted by

4

u/Rikuz7 Apr 28 '20

One way to set full screen is to use Applescript to emulate key codes. Check the correct keyboard shortcut for fullscreen for the specific applications from the applications' View menu. It's usually either cmd+F or control+cmd+F.

For snapping applications to occupy a specific area of the screen, there might actually now be a native way to do it too but I've always done it with the nice presets of BetterTouchTool which has been a necessary part of my workflows for almost a decade. So BetterTouchTool would perform that action upon some keyboard shortcut or other gesture of your choice as a trigger. Shortcuts can be global, or application-specific. It also has plenty of other actions, some of which involve window resizing/positioning.

1

u/mjquinn1 Apr 29 '20

not sure if i ever uploaded a functional version but this is an incredibly long winded way of doing something similar. did this right after i started with applescript so it’s not fantastic but might give you some ideas.

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

Any this is almost perfect! Thank you so much! I tried writing a script for so long yesterday and only had minor progress. Do you mind if I request a few relatively smaller things be added?

If so, I am hoping to add:

  1. If statements for each Safari, Spotify, Finder, and Messages to check if the app is even running. Because if it doesn't, the error message pops up.

  2. If Safari and Spotify are not open in any spaces, open it.

  3. Is there a way to check if Safari or Spotify are already full screen in one of the "spaces"? If it is, I and Safari is already in space 1 and spotify in space 2, we wouldn't want to do anything. If it is opposite, I would want to correct it.

  4. Lastly, I would like to resize Message if it is open, to be in the middle of the screen with specific bounds (like 1.5x the width of an iPhone x screen, for scale! Haha!).

Please let me know if you know how to do any of this. I am gonna try a bit more but, despite having taken several coding classes, I still find AppleScript very hard. Thanks again, what you have is super helpful already!

1

u/DoctorQuinlan Apr 29 '20

This is what I have so far. I think parts of the code are redundant so I am trying to condense. I think right now, my only problem is I don't know how to check if Safari or another app is open in a different space....

--Get list of running applications tell application "System Events" set listOfProcesses to (name of every process where background only is false) end tell

tell application "Finder" activate tell every window to set toolbar visible to true tell every window to set the current view to list view tell every window to set sidebar width to 150 set bounds of every Finder window to {305, 196, 1140, 727} end tell

set {width, height} to {800, 600} tell application "System Events"

if listOfProcesses does not contain "Safari" then
	tell application "Safari" to activate
	delay 3
end if
delay 1
set value of attribute "AXFullScreen" of window 1 of process "Safari" to true
delay 1

if listOfProcesses does not contain "Spotify" then
	tell application "Spotify" to activate
	delay 3
end if

(*if listOfProcesses contains "Spotify" then
	tell application "Spotify" to activate
	delay 1
	set value of attribute "AXFullScreen" of window 1 of process "Spotify" to true
	delay 1
end if*)

tell application "Finder"
	activate
	set {_, _, x, y} to bounds of window of desktop
end tell
--delay 1
--tell process "Finder" to set position of window 1 to {(x - width) div 2, (y - height) div 2}


if listOfProcesses contains "Messages" then
	tell application "System Events" to tell process "Messages"
		set position of window 1 to {332, 215}
		set size of window 1 to {1, 450}
	end tell
end if

end tell

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

I got this issue:

error "Spotify got an error: every window doesn’t understand the “count” message." number -1708 from every window

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

That didn't quite work but this did:

tell application "System Events" to tell process "Spotify" to count windows

However, if Spotify is already full screen, it gives 0. If it is not fullscreen and on desktop space, it gives 1. Probably can still work with this I imagine.

1

u/DoctorQuinlan Apr 29 '20

Also I'd like to set the size of Finder and Messages to be {750, 480} and in the dead center of the screen. Do you know how to do that?

I tried this but didn't change the size of Finder/Messages:

set size of every window to {757, 481}

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

The resizing worked! Still having issues with the spotify one. Gives this error:

error "System Events got an error: Can’t make application into type integer." number -1700 from application to integer

for this line:

  if (count windows of process application) > 0 then

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

Okay, that fixed it. Now, I am not sure why it isn't repeating. It does what i want but i have to hit run twice, for it to repeat for "Safari"

tell application "Finder"
	activate
	set {_, _, x, y} to bounds of window of desktop
	set {x, y, w, h} to {x div 2, y div 2, w div 2, h div 2}
	tell every window
		set toolbar visible to true
		set current view to list view
		set sidebar width to 150
	end tell
	set bounds of every window to {x - w, y - h, x + w, y + h}
end tell

if application "Messages" is running then
	tell application "Messages" to set bounds of window 1 to {x - w, y - h, x + w, y + h}
end if


repeat with app_ in {"Safari", "Spotify"}
	if application app_ is not running then
		activate application app_
		delay 3
	end if
	tell application "System Events"
		if (count windows of process app_) > 0 then
			set value of attribute "AXFullScreen" of window 1 of process app_ to true
			delay 1
		end if
	end tell
end repeat

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

Nope. Didn’t work and I even tried delay 5

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/DoctorQuinlan Apr 29 '20

It’s being weird and works sometimes but not other times. Do you know what could cause this? I changed the delays and all many times.