-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.applescript
304 lines (283 loc) · 10.4 KB
/
lib.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(*
AppleScript utility handlers
A drop-in library of modern AppleScript utility functions
Version 0.1
APL 2.0
*)
-- Compile AppleScript
-- Perform compilation on .AppleScript file to make it into a binary
on compileAppleScript(sourcePath, destPath)
set shellCommand to "osacompile -o " & destPath & " " & sourcePath
try
do shell script shellCommand
on error errMsg number errNum
display dialog "An error occurred: " & errMsg & " (Error " & errNum & ")"
end try
end compileAppleScript
-- Macintosh-flavor paths
-- Convert between classic macintosh-style and unix-style paths, and vice-versa
on convertPathToUnixStyle(macPath)
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set pathItems to text items of macPath
set AppleScript's text item delimiters to "/"
set unixPath to "/" & pathItems as text
set AppleScript's text item delimiters to oldDelims
return unixPath
on error errMsg number errNum
display dialog "An error occurred: " & errMsg & " (Error " & errNum & ")"
end try
end convertPathToUnixStyle
on convertPathToMacStyle(unixPath)
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set pathItems to text items of unixPath
set AppleScript's text item delimiters to ":"
set macPath to pathItems as text
set AppleScript's text item delimiters to oldDelims
return macPath
on error errMsg number errNum
display dialog "An error occurred: " & errMsg & " (Error " & errNum & ")"
end try
end convertPathToMacStyle
-- Switch the current app
-- Switch to a particular app name, and optionally start the app if it isn't open yet
on switchApp(appName, startApp)
try
tell application "System Events"
if exists application process appName then
if not (exists (process appName)) then
if startApp is true then
tell application appName to activate
else
display dialog appName & " is not currently running, and the script is set not to open it."
end if
else
set frontmost of process appName to true
end if
else
display dialog "The application " & appName & " could not be found."
end if
end tell
on error errMsg
display dialog "Error switching to app: " & errMsg
end try
end switchApp
-- Cycle through windows of an app
-- Switch to a window number, or to a particular matching window title string
on switchWindow(appName, targetTitle, windowNumber)
try
set targetApp to ""
if appName is "" then
tell application "System Events"
set targetApp to name of first application process whose frontmost is true
end tell
else
set targetApp to appName
end if
tell application targetApp
set windowCount to count of windows
if windowCount > 1 then
if targetTitle is not "" then
repeat with i from 1 to windowCount
if name of window i is targetTitle then
set index of window i to 1
return "Switched to window '" & targetTitle & "' in " & targetApp & "."
end if
end repeat
return "No window with title '" & targetTitle & "' found in " & targetApp & "."
else if windowNumber is not "" then
if windowNumber > 0 and windowNumber <= windowCount then
set index of window windowNumber to 1
return "Switched to window " & windowNumber & " in " & targetApp & "."
else
return "Invalid window number. Choose a number between 1 and " & windowCount & "."
end if
else
set index of window 1 to windowCount
return "Switched to window " & windowCount & " in " & targetApp & "."
end if
else
return "There's only one window open for " & targetApp & "."
end if
end tell
on error errMsg
return "Error switching window: " & errMsg
end try
end switchWindow
-- Get file or folder size
-- Use Finder to get the size of a file or folder, as an integer
on getSize(itemPath)
try
tell application "Finder"
get size of (POSIX file itemPath as alias)
end tell
on error errMsg
display dialog "Error getting size of the item: " & errMsg
end try
end getSize
-- Read file
-- Safely read from a file
on readTextFile(filePath)
try
set fileContent to read file filePath as text
on error errMsg number errNum
display dialog "Error reading file: " & filePath & return & "Error Message: " & errMsg & " | Error Number: " & errNum
return ""
end try
return fileContent
end readTextFile
-- Write to file
-- Safely write content to a text file, with an option to either overwrite or append to an existing file's contents
on writeTextToFile(content, filePath, overwrite)
try
set fileDescriptor to open for access file filePath with write permission
if overwrite is true then
set eof of fileDescriptor to 0 -- if overwrite is true, reset the end of file marker to 0
end if
write content to fileDescriptor starting at eof -- write content, if overwrite is false, it will start at eof and append
close access fileDescriptor
on error errMsg
display dialog "Error writing to file: " & errMsg
try
close access file filePath
end try
end try
end writeTextToFile
-- Delete file
-- Either move a file to trash or skip the trash, deleting it permanently
on deleteItem(itemPath, permanent)
try
if permanent is true then
do shell script "rm " & quoted form of POSIX path of itemPath
else
tell application "Finder"
delete POSIX file itemPath
end tell
end if
on error errMsg
display dialog "Error deleting item: " & errMsg
end try
end deleteItem
-- Create notification
on showNotification(title, message)
if title is not "" and message is not "" then
try
display notification message with title title
on error errMsg
display dialog "Failed to display notification: " & errMsg
end try
else
display dialog "Error: Both title and message must be non-empty strings."
end if
end showNotification
-- Toggle dark mode
-- Switch between dark and light mode, including forcing any apps that adhere to the system setting for color mode
on toggleDarkMode()
try
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell
on error errMsg
display dialog "Error toggling dark mode: " & errMsg
end try
end toggleDarkMode
-- Timer
-- Display a timed dialog box, setting a timer in seconds
on displayTimedDialog(message, seconds)
try
if seconds < 0 then error "Negative time is not valid"
display dialog message giving up after seconds
on error errMsg
display dialog "Error displaying timed dialog: " & errMsg
end try
end displayTimedDialog
-- Set volume
-- Set system volume as a 0 to 100 integer value
on setVolume(level)
try
if level < 0 or level > 100 then error
set volume output volume level
on error
display dialog "Error: Invalid volume level. Please enter a number between 0 and 100."
end try
end setVolume
-- Open URL
-- Open an address in the default browser, or specify a browser
on openURL(url, browser)
try
if browser is "" or browser is missing value then
-- use default browser
tell application "System Events"
open location url
end tell
else
-- use custom browser
tell application browser
open location url
end tell
end if
on error errMsg
display dialog "Error opening URL: " & errMsg
end try
end openURL
-- Set desktop background
on setDesktopPicture(filePath)
try
tell application "Finder"
set desktop picture to POSIX file filePath
end tell
on error errMsg
display dialog "Error setting desktop picture: " & errMsg
end try
end setDesktopPicture
-- Eject all external disks
on ejectAllDisks()
try
tell application "Finder"
eject (every disk whose ejectable is true)
end tell
on error errMsg
display dialog "Error ejecting disks: " & errMsg
end try
end ejectAllDisks
-- Set custom sleep
on setSleepSettings(displaySleepMinutes, diskSleepMinutes)
try
do shell script "sudo pmset displaysleep " & displaySleepMinutes & " disksleep " & diskSleepMinutes with administrator privileges
on error errMsg
display dialog "Error setting sleep settings: " & errMsg
end try
end setSleepSettings
-- Quit all apps
on quitAllApps()
try
tell application "System Events"
set allApps to name of every application process whose background only is false
repeat with appName in allApps
tell application appName to quit
end repeat
end tell
on error errMsg
display dialog "Error quitting apps: " & errMsg
end try
end quitAllApps
-- Return the current wifi network
on getCurrentWiFi()
try
do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
on error errMsg
-- Convert image
-- Convert an image into another image, changing the types, most often used to convert HEIC to PNG or JPG
on convertImage(sourcePath, destinationPath, format)
try
if format is not in {"jpeg", "png"} then error "Invalid format. Please specify 'jpeg' or 'png'."
do shell script "sips -s format " & format & " " & quoted form of sourcePath & " --out " & quoted form of destinationPath
on error errMsg
display dialog "Error converting image: " & errMsg
end try
end convertImage