forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquit-application.applescript
executable file
·93 lines (75 loc) · 2.24 KB
/
quit-application.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
#!/usr/bin/osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Quit Application
# @raycast.mode silent
# @raycast.packageName Navigation
# Optional parameters:
# @raycast.icon images/quit.png
# @raycast.argument1 { "type": "text", "placeholder": "Name", "optional": true }
# @raycast.argument2 { "type": "text", "placeholder": "Force quit?", "optional": true }
# Documentation:
# @raycast.author Jakub Lanski
# @raycast.authorURL https://github.com/jaklan
# @raycast.description Quit the application. Edit the command to change the default values (Application: "", Force quit?: "No").
on run argv
### Configuration ###
set defaultApp to ""
set defaultForceQuit to false
### End of configuration ###
try
set app_ to getApplication(item 1 of argv, defaultApp)
set forceQuit to getForceQuit(item 2 of argv, defaultForceQuit)
quitApplication(app_, forceQuit)
on error errorMessage
return errorMessage
end try
end run
### Functions ###
on getApplication(query, defaultApp)
set app_ to query
if app_ = "" then
set app_ to defaultApp
if app_ = "" then error "Default application is not set, edit the command file"
else if not applicationExists(query) then
set app_ to findMatchingApplication(query)
if app_ = "" then error "No application matching \"" & query & "\" is running"
end if
return app_
end getApplication
on applicationExists(app_)
tell application "System Events"
return (first process whose name = app_) exists
end tell
end applicationExists
on findMatchingApplication(query)
tell application "System Events"
try
return name of first process whose name contains query
on error
return ""
end try
end tell
end findMatchingApplication
on getForceQuit(arg, defaultForceQuit)
set forceQuit to arg
if forceQuit = "" then
set forceQuit to defaultForceQuit
else if forceQuit is in {"no", "n"} then
set forceQuit to false
else if forceQuit is in {"yes", "y"} then
set forceQuit to true
else
error "Wrong value of the \"Force quit?\" argument: use \"y(es)\" or \"n(o)\""
end if
return forceQuit
end getForceQuit
on quitApplication(app_, forceQuit)
tell application app_
if not forceQuit then
quit
else
do shell script "pkill -i " & app_
end if
end tell
end quitApplication