Created
December 31, 2017 06:02
-
-
Save krazdax5/30ad2ee228cee6ac1c2581ff2ff7ccf4 to your computer and use it in GitHub Desktop.
Automation script to open a new space on macOS using spotlight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
* Automation script that opens a new space on the current monitor | |
* based on the current position of the mouse pointer | |
* | |
* Created by: Charles Levesque | |
* on: december 31st, 2017 | |
* last modified: december 31st, 2017 | |
* version: 1.0 | |
* | |
* | |
* Copyright 2017 Charles Levesque | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*) | |
use AppleScript version "2.7" | |
use scripting additions | |
use framework "Cocoa" | |
global LOGGING_ENABLED | |
set LOGGING_ENABLED to true | |
(* | |
* Logs a message if logging is enabled | |
* | |
* :param message: Message to be logged. | |
*) | |
on logg about message | |
if LOGGING_ENABLED then | |
tell me to log message | |
end if | |
end logg | |
(* | |
* Gets the current position of the mouse pointer on screen. | |
* | |
* :return: A NSPoint containing the cursor's location. | |
*) | |
on getCurrentCursorPosition() | |
return current application's NSEvent's mouseLocation() | |
end getCurrentCursorPosition | |
(* | |
* Get the screens width list for current available displays. | |
* | |
* :return: A list of real reprensenting the displays' width in order. | |
*) | |
on getCurrentScreensWidth() | |
set screens to current application's NSScreen's screens() | |
set widths to {} | |
repeat with curScreen in screens | |
set frame to curScreen's frame() | |
copy item 1 of item 2 of frame to the end of widths | |
end repeat | |
return widths | |
end getCurrentScreensWidth | |
-- main | |
on main given logging:isLoggingEnabled : true | |
set LOGGING_ENABLED to isLoggingEnabled | |
set currentCursorPosition to my getCurrentCursorPosition() | |
set currentWidths to my getCurrentScreensWidth() | |
set curX to x of currentCursorPosition | |
set accumulator to 0 | |
local spacesGroup | |
logg of me about "Mouse location: x=" & x of currentCursorPosition & ", y=" & y of currentCursorPosition | |
logg of me about "Screens widths: " & currentWidths | |
repeat with groupIndex from 1 to count of currentWidths | |
set width to item groupIndex of currentWidths | |
set accumulator to (accumulator + width) | |
if curX is less than or equal to accumulator then | |
set spacesGroup to groupIndex | |
exit repeat | |
end if | |
end repeat | |
logg of me about "Selected group: " & spacesGroup | |
tell application "System Events" | |
do shell script "open -a 'Mission Control'" | |
delay 0.5 | |
tell process "Dock" | |
set spacesBarGroup to group "Spaces Bar" of group spacesGroup of group "Mission Control" | |
click (every button whose value of attribute "AXDescription" is "add desktop") of spacesBarGroup | |
end tell | |
end tell | |
tell current application to quit | |
end main | |
main of me with logging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment