forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart-application.applescript
executable file
·103 lines (83 loc) · 2.44 KB
/
restart-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
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Restart Application
# @raycast.mode silent
# @raycast.packageName Navigation
# Optional parameters:
# @raycast.icon images/restart.png
# @raycast.argument1 { "type": "text", "placeholder": "Name", "optional": true }
# @raycast.argument2 { "type": "text", "placeholder": "Force restart?", "optional": true }
# Documentation:
# @raycast.author Jakub Lanski
# @raycast.authorURL https://github.com/jaklan
# @raycast.description Restart the application. Edit the command to change the default values (Application: "", Force restart?: "No").
on run argv
### Configuration ###
set defaultApp to ""
set defaultForceRestart to false
### End of configuration ###
try
set app_ to getApplication(item 1 of argv, defaultApp)
set forceRestart to getForceRestart(item 2 of argv, defaultForceRestart)
restartApplication(app_, forceRestart)
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 getForceRestart(arg, defaultForceRestart)
set forceRestart to arg
if forceRestart = "" then
set forceRestart to defaultForceRestart
else if forceRestart is in {"no", "n"} then
set forceRestart to false
else if forceRestart is in {"yes", "y"} then
set forceRestart to true
else
error "Wrong value of the \"Force restart?\" argument: use \"y(es)\" or \"n(o)\""
end if
return forceRestart
end getForceRestart
on restartApplication(app_, forceRestart)
tell application app_
if not forceRestart then
quit
else
do shell script "pkill -i " & app_
end if
repeat
if not it is running then
exit repeat
else
delay 0.1
end if
end repeat
activate
end tell
end restartApplication