Archive for the 'NSTimer' Category

2015/11/09 ムービーからオーディオのみ抽出してm4aで保存 v2

指定のQuickTimeムービーからオーディオのみ抽出してm4a(MPEG-4 Audio)で保存するAppleScriptです。

Shane Stanleyから「外部のFrameworkを使わなくても、AppleScriptだけで処理できる」バージョンを教えてもらいました。なるほど、たしかに外部のFrameworkを利用しなくてもexportできることを確認しました。

本Scriptは2本ともForegroundで実行する必要があるため、Script EditorでCommand-Control-Rで実行します。ASObjC Explorer 4では、「Run in Foreground」にチェックを入れて実行します。

AppleScript名:ASOCでムービーからオーディオのみ抽出してM4Aで保存 v2a
– Created 2015-11-09 by Shane Stanley
use AppleScript version “2.3.1″
use scripting additions
use framework “Foundation”
use framework “AVFoundation”

set aFile to POSIX path of (choose file of type {“com.apple.quicktime-movie”} with prompt “Choose a QuickTime movie to export as MP4 audio”)
set anNSURL to current application’s class “NSURL”’s fileURLWithPath:aFile
set outNSURL to anNSURL’s URLByAppendingPathExtension:“m4a”

set theAsset to current application’s AVAsset’s assetWithURL:anNSURL
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:(current application’s AVAssetExportPresetAppleM4A)
theSession’s setOutputURL:outNSURL
theSession’s setOutputFileType:(current application’s AVFileTypeAppleM4A)
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
current application’s NSTimer’s scheduledTimerWithTimeInterval:2.0 target:me selector:“checkSession:” userInfo:theSession repeats:true

on checkSession:theTimer
  set theSession to theTimer’s userInfo()
  
set theProgress to theSession’s status() as integer
  
if theProgress > 2 then
    theTimer’s invalidate()
    
if theProgress = (current application’s AVAssetExportSessionStatusCompleted) as integer then
      display dialog “Done”
    else if theProgress = (current application’s AVAssetExportSessionStatusFailed) as integer then
      display dialog “Failed”
    else if theProgress = (current application’s AVAssetExportSessionStatusCancelled) as integer then
      display dialog “Cancelled”
    end if
  end if
end checkSession:

★Click Here to Open This Script 

AppleScript名:ASOCでムービーからオーディオのみ抽出してM4Aで保存 v2b
– Created 2015-11-09 by Shane Stanley
– Modified 2015-11-09 by Takaaki Naganoya
use AppleScript version “2.3.1″
use scripting additions
use framework “Foundation”
use framework “AVFoundation”

property expStat : false

set aFile to POSIX path of (choose file of type {“com.apple.quicktime-movie”} with prompt “Choose a QuickTime movie to export as MP4 audio”)
set anNSURL to current application’s class “NSURL”’s fileURLWithPath:aFile
set outNSURL to anNSURL’s URLByAppendingPathExtension:“m4a”

set theAsset to current application’s AVAsset’s assetWithURL:anNSURL
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:(current application’s AVAssetExportPresetAppleM4A)
theSession’s setOutputURL:outNSURL
theSession’s setOutputFileType:(current application’s AVFileTypeAppleM4A)
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
current application’s NSTimer’s scheduledTimerWithTimeInterval:1.0 target:me selector:“checkSession:” userInfo:theSession repeats:true

repeat
  delay 0.01
  
if my expStat is not equal to false then exit repeat
end repeat

return my expStat
–>  ”Done”

on checkSession:theTimer
  set theSession to theTimer’s userInfo()
  
set theProgress to theSession’s status() as integer
  
if theProgress > 2 then
    theTimer’s invalidate()
    
if theProgress = (current application’s AVAssetExportSessionStatusCompleted) as integer then
      set my expStat to “Done”
    else if theProgress = (current application’s AVAssetExportSessionStatusFailed) as integer then
      set my expStat to “Failed”
    else if theProgress = (current application’s AVAssetExportSessionStatusCancelled) as integer then
      set my expStat to “Cancelled”
    end if
  end if
end checkSession:

★Click Here to Open This Script 

2011/01/16 タイマー割り込みでマウスの座標を取得する

NSTimerで宣言したタイマー割り込みサービスで定期的に(0.1秒ごとに)マウスカーソルの座標を取得するサンプルです。

昔、このために専用のアプリが配布されていましたが……AppleScriptでこんなに簡単に実現できるようになった、というのはすごいことです。以前から不満に思っていた機能が割とあるので、このサンプルをもとにいろいろ追加してしまいました。

→ mousewatcher.zip