NSRunningApplication

バンドルIDからプロセス番号を調べる


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004#
005#
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use framework "UniformTypeIdentifiers"
011use scripting additions
012
013property refMe : a reference to current application
014
015log refMe's NSVolumeEnumerationProduceFileReferenceURLs as integer
016
017#調べるバンドルID
018set strBundleID to "com.adobe.Acrobat.Pro" as text
019
020#バンドルIDで起動中のアプリを調べる
021set ocidAppListArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)
022set numCntApp to ocidAppListArray's |count|()
023#起動中を確認
024if numCntApp > 0 then
025  #起動してい場合はリストの最初の項目
026  set appRunningApp to ocidAppListArray's firstObject()
027  #プロセスIDを取得
028  set numPID to appRunningApp's processIdentifier()
029  log "プロセスIDは: " & numPID
030  
031else if numCntApp = 0 then
032  return strBundleID & "は起動していません"
033end if
AppleScriptで生成しました

|

起動中の関連プロセスを終了させる(考え中)


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# スクリプトメニューからの実行用
005# プロセス番号取得してKILLまではしない方式
006# 他にいい方法がありそうだけど 現時点でのベターな方法
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "UniformTypeIdentifiers"
012use framework "AppKit"
013use scripting additions
014property refMe : a reference to current application
015
016##設定項目 対象のバンドルIDのドメイン部
017set strKeyWord to ("adobe") as text
018
019
020set numCntApp to 1
021repeat until numCntApp = 0
022  ##対象プロセスが無くなるまで繰り返し
023  set numCntApp to doAppQuit(strKeyWord)
024  delay 1
025end repeat
026
027
028
029###############
030#サブルーチン
031###############
032to doAppQuit(argKeyWord)
033  #全プロセス取得して
034  tell application "System Events"
035    #バンドルIDリストにします
036    set listBundleID to bundle identifier of (every application process)
037  end tell
038  #全プロセスで
039  repeat with itemBundleID in listBundleID
040    #対象のドメイン名を含むなら
041    if itemBundleID contains argKeyWord then
042      #バンドルIDでアプリケーションを取得して
043      set ocidResultsArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID))
044      set numCntArray to ocidResultsArray's |count|()
045      repeat with itemNo from 0 to (numCntArray - 1) by 1
046        set ocidRunApp to (ocidResultsArray's objectAtIndex:(itemNo))
047        #通常終了を試みます
048        set boolDone to ocidRunApp's terminate()
049        if (boolDone) is true then
050          log itemBundleID & ":正常終了"
051          #失敗したら
052        else if (boolDone) is false then
053          #強制終了を試みます
054          set boolDone to ocidRunApp's forceTerminate()
055          if (boolDone) is true then
056            log itemBundleID & ":強制終了"
057          else if (boolDone) is false then
058            log itemBundleID & ":終了出来ませんでした"
059          end if
060        end if
061      end repeat
062    end if
063  end repeat
064  #実行後のプロセス数で
065  tell application "System Events"
066    set listBundleID to bundle identifier of (every application process)
067  end tell
068  set numDoneCnt to 0 as integer
069  repeat with itemBundleID in listBundleID
070    #対象のドメイン名を含む数を
071    if itemBundleID contains "adobe" then
072      set numDoneCnt to numDoneCnt + 1
073    end if
074  end repeat
075  #戻す
076  return numDoneCnt
077end doAppQuit
AppleScriptで生成しました

|

バンドル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

|

その他のカテゴリー

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