AppleScript NSRunningApplication

バンドルIDから再起動


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


set strBundleID to "com.apple.dock"

########################
## 再起動
########################
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
##バンドルからアプリケーションのURLを取得
set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
if ocidAppBundle ≠ (missing value) then
  set ocidAppPathURL to ocidAppBundle's bundleURL()
else if ocidAppBundle = (missing value) then
  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
end if
##予備(アプリケーションのURL)
if ocidAppPathURL = (missing value) then
  tell application "Finder"
    try
      set aliasAppApth to (application file id strBundleID) as alias
      set strAppPath to POSIX path of aliasAppApth as text
      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
      set strAppPath to strAppPathStr's stringByStandardizingPath()
      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
    on error
return "アプリケーションが見つかりませんでした"
    end try
  end tell
end if
##Dock終了
set ocidRunningApplication to refMe's NSRunningApplication
###起動中のすべてのリスト
set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID))
###複数起動時も順番に終了
repeat with itemAppArray in ocidAppArray
  ###終了
itemAppArray's terminate()
end repeat
##1秒まって終了を確認
delay 1
##終了できない場合は強制終了
repeat with itemAppArray in ocidAppArray
  set boolTerminate to itemAppArray's terminated
  if boolTerminate = false then
itemAppArray's forceTerminate()
  end if
end repeat

###起動
set ocidOpenConfig to refMe's NSWorkspaceOpenConfiguration's configuration
###コンフィグ
(ocidOpenConfig's setActivates:(refMe's NSNumber's numberWithBool:true))
###起動
(appSharedWorkspace's openApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfig) completionHandler:(missing value))

return "再起動"

|

[processIdentifier]プロセスIDの取得


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application

set appWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidRunApp to refMe's NSRunningApplication
set ocidAppArray to appWorkspace's runningApplications()

repeat with itemAppArray in ocidAppArray
  log itemAppArray's localizedName() as text
  log itemAppArray's bundleIdentifier() as text
  log itemAppArray's processIdentifier() as text
  
end repeat

|

[wacom]関連プロセス終了


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set listUTI to {"com.wacom.TabletDriver", "com.wacom.DataStoreMgr", "com.wacom.FirmwareUpdater", "com.wacom.IOManager", "com.wacom.MultiTouch", "com.wacom.ProfessionalControlPanel", "com.wacom.RemoveTabletHelper", "com.wacom.RemoveWacomTablet", "com.wacom.TabletDriver", "com.wacom.TabletHelper", "com.wacom.UpdateHelper", "com.wacom.UpgradeHelper", "com.wacom.Wacom-Desktop-Center", "com.wacom.Wacom-Display-Settings", "com.wacom.WacomCenter", "com.wacom.WacomCenterPrefPane", "com.wacom.WacomExperienceProgram", "com.wacom.WacomTabletDriver", "com.wacom.WacomTouchDriver", "com.wacom.displayhelper", "com.wacom.wacomtablet", "com.wacom.DisplayMgr"} as list

repeat with itemUTI in listUTI
  ###NSRunningApplication
  set ocidRunningApplication to refMe's NSRunningApplication
  ###起動中のすべてのリスト
  set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI))
  ###複数起動時も順番に終了
  repeat with itemAppArray in ocidAppArray
itemAppArray's terminate()
  end repeat
end repeat
log "通常終了"
delay 3

repeat with itemUTI in listUTI
  ###NSRunningApplication
  set ocidRunningApplication to refMe's NSRunningApplication
  ###起動中のすべてのリスト
  set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI))
  ###複数起動時も順番に終了
  repeat with itemAppArray in ocidAppArray
itemAppArray's forceTerminate()
  end repeat
end repeat

log "強制終了"




|

実行中のスクリプト+スクリプトモニタを終了させる


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###スクリプトモニタ終了
set listUTI to {"com.apple.ScriptMonitor"}
repeat with itemUTI in listUTI
  ###NSRunningApplication
  set ocidRunningApplication to refMe's NSRunningApplication
  ###起動中のすべてのリスト
  set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI))
  ###複数起動時も順番に終了
  repeat with itemAppArray in ocidAppArray
itemAppArray's terminate()
  end repeat
end repeat
###スクリプト終了
set strCommandText to ("/bin/ps -alx | grep osascript | grep -v grep| awk '{print $2}'")
set strPID to (do shell script strCommandText) as text
set strCommandText to ("/bin/kill -9 " & strPID & "") as text
do shell script strCommandText
###
#set ocidApp to (ocidRunningApplication's runningApplicationWithProcessIdentifier:(strPID))
#ocidApp's terminate()


|

[NSRunningApplication]アプリケーションの終了


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set strUTI to "com.apple.Preview"

###NSRunningApplication
set ocidRunningApplication to refMe's NSRunningApplication
###起動中のすべてのリスト
set ocidAppArray to ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strUTI)
###複数起動時も順番に終了
repeat with itemAppArray in ocidAppArray
itemAppArray's terminate()
end repeat


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set listUTI to {"com.google.Chrome", "com.google.Chrome.helper.renderer", "com.google.GoogleUpdater", "com.google.Chrome.helper", "com.google.Chrome.helper.plugin", "chrome_crashpad_handler"}

repeat with itemUTI in listUTI
  try
    log itemUTI
    tell application id itemUTI to quit
  end try
end repeat

repeat with itemUTI in listUTI
  ###NSRunningApplication
  set ocidRunningApplication to refMe's NSRunningApplication
  ###起動中のすべてのリスト
  set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI))
  log itemUTI
  ###複数起動時も順番に終了
  repeat with itemAppArray in ocidAppArray
itemAppArray's terminate()
  end repeat
end repeat

|

[NSRunningApplication]起動中か確認

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set strUTI to "com.adobe.Acrobat.Pro" as text

set ocidRunAppArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strUTI)
if (count of ocidRunAppArray) ≠ 0 then
  log ocidRunApp's localizedName as text
  log "起動中です"
else
return "起動していません"
end if


|

[NSRunningApplication]Finderを再起動させる

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

# 

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application


##############################################

## Finderを再起動

##############################################


set ocidAppList to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder"

if (count of ocidAppList) ≠ 0 then

  ###Finderを取得して

  set ocidAppFinder to ocidAppList's objectAtIndex:0

  ####終了させて

  ocidAppFinder's terminate()

  ###2秒まって

  delay 2

  ###起動させる

  set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

  ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

else if (count of ocidAppList) = 0 then

  ###Finderが無いなら起動

  set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

  ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

end if

#############起動確認 10回10秒間

repeat 10 times

  set ocidAppList to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder"

  if (count of ocidAppList) = 0 then

    ###Finderが無いなら起動

    set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

    ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

  else

    ###あるなら起動確認終了

    exit repeat

  end if

  delay 1

end repeat

|

[NSRunningApplication]起動中のアプリケーションの終了

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

set objFileManager to refMe's NSFileManager's defaultManager()

doQuitApp2UTI("org.mozilla.firefox")
doQuitApp2UTI("com.google.Chrome")
doQuitApp2UTI("org.chromium.Chromium")
doQuitApp2UTI("com.microsoft.edgemac")
doQuitApp2UTI("com.apple.Safari")
doQuitApp2UTI("com.apple.dt.Xcode")
doQuitApp2UTI("com.apple.MobileSMS")





###################################
########アプリケーションを終了させる
###################################
to doQuitApp2UTI(argUTI)
set strUTI to argUTI as text
set ocidResultsArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
set numCntArray to ocidResultsArray count
if numCntArray0 then
set ocidRunApp to ocidResultsArray's objectAtIndex:0


###通常終了
set boolDone to ocidRunApp's terminate()

####強制終了
ocidRunApp's forceTerminate()



#### killallを使う場合
set ocidExecAppURL to ocidRunApp's executableURL()
set ocidFileName to ocidExecAppURL's lastPathComponent()
set strFileName to ocidFileName as text

set strCommandText to ("/usr/bin/killall -z " & strFileName & "") as text
set ocidCommandText to refNSString's stringWithString:strCommandText
set ocidTermTask to refMe's NSTask's alloc()'s init()
ocidTermTask's setLaunchPath:"/bin/zsh"
ocidTermTask's setArguments:({"-c", ocidCommandText})
set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)


####killを使う場合
set ocidPID to ocidRunApp's processIdentifier()
set strPID to ocidPID as text
log strPID
set strCommandText to ("/bin/kill -9 " & strPID & "") as text
set ocidCommandText to refNSString's stringWithString:strCommandText
set ocidTermTask to refMe's NSTask's alloc()'s init()
ocidTermTask's setLaunchPath:"/bin/zsh"
ocidTermTask's setArguments:({"-c", ocidCommandText})
set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)




end if
end doQuitApp2UTI

|

[AppKit] hide() unhide() isHidden()

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property objMe : a reference to current application

######ログ表示
doLogView()

####UTI指定
set strUTI to "com.apple.TextEdit"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI

####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
(*
複数起動できるコマンド等は
リストを順に取得する事になる
*)

#### hide 隠しているしているか?調べる
log ocidObjApp's hide as boolean
#### unhide
log ocidObjApp's unhide as boolean
#### isHidden
log ocidObjApp's isHidden as boolean

## false-->起動中
## true-->起動していない(終了している)


####実際に隠す
ocidObjApp's hide

####2秒まって
delay 2

####隠すをやめる
ocidObjApp's unhide





#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppKit] isTerminated 終了しているか?=起動していない

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property objMe : a reference to current application

######ログ表示
doLogView()

####UTI指定
set strUTI to "com.apple.finder"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI

####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
(*
複数起動できるコマンド等は
リストを順に取得する事になる
*)

####終了しているか?調べる
log ocidObjApp's isTerminated as boolean

## false-->起動中
## true-->起動していない(終了している)



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom