Monday, October 26, 2020

#1 2020-10-15 05:25:45 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1352

Shutdown Application with Timer (AsObjC)

One small but cool script I just wrote. Its advantage over the versions already available on the network is that it relieves the user of the need to manually type the application name.

Applescript:


--- Script for when you want to close an application in the future, but you don't want to be bothered to remember to do it
use framework "Foundation"
use scripting additions
property curApp : a reference to current application
property NSWorkspace : a reference to NSWorkspace of curApp
property NSMutableArray : a reference to NSMutableArray of curApp
property NSURLApplicationIsScriptableKey : a reference to NSURLApplicationIsScriptableKey of curApp
property |timeout| : 60 -- minutes

set appList to (NSWorkspace's sharedWorkspace())'s runningApplications()
set appNames to NSMutableArray's array()
repeat with anApp in appList
   try
       set {theResult, theError} to ((anApp's bundleURL())'s getResourceValue:(reference) forKey:NSURLApplicationIsScriptableKey |error|:(missing value))
       if theResult as boolean then (appNames's addObject:(anApp's localizedName()))
   end try
end repeat

set applicationName to choose from list (appNames as list) ¬
   with prompt "Choose application you want to shut down after " & |timeout| & " minutes(s)"
if applicationName is false then return
set applicationName to item 1 of applicationName

delay minutes * |timeout|
tell application applicationName to quit

Last edited by KniazidisR (2020-10-15 05:28:12 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)