Archive for the 'FSEvents' Category

2015/03/05 特定フォルダの更新通知を受け、特定ファイルが存在しているかを確認

I made an AppleScript program to download data file by using Safari. And the program detect the downloaded file then rename it and move it to another folder.

The program use OS built-in Folder Action Script mechanism (well known). Main program and Folder Action Script has a simple sync way using a plist to share the renaming rule and the state of renaming (finished or not). So, programs have to access the plist very frequently.

But sometimes Finder crashed. So, the process has been stopped.

I had to study another way to detect downloaded file. Notification seems very smart. Except FSEvents have to use blocks construction which can not be written in AppleScriptObjC.

This ASOC on Xcode project is originally written by StefanK@MacScripters. I changed a little (ARC etc.).


Safariを使ってデータファイルをダウンロードするAppleScriptを作成。同プログラムは、Folder Action Scriptを使ってダウンロードされたファイルを検出し、リネームしたうえで別フォルダに移動するようになっていました。


オリジナルのASOC on Xcodeのプログラムは、MacScriptersのStefanK氏が作ったものです。ARCに対応させるためにObjective-Cのプログラムを少しだけ書き換えました。


→ Download Xcode Project (33KB)


– AppDelegate.applescript
– dirWatcher

– Created by StefanK on 2013-03-11 02:05:32 am

– Changed by Takaaki Naganoya on 2015-03-05
– Copyright (c) 2015年 Takaaki Naganoya. All rights reserved.

script AppDelegate
property parent : class “NSObject”
property DirectoryWatcher : class “FSEDirectoryWatcher”
property statusImage1 : missing value – image view 1
property statusImage2 : missing value – image view 2
property watchFileName : “” –File Name to detect the existence
on applicationWillFinishLaunching:aNotification
set my watchFileName to “logfile0.log”
–Get Safari Download Folder Path
set preferencesPath to POSIX path of (path to preferences)
set bundleId to bundle identifier of (info for (path to application “Safari”))
set safariPrefsFile to preferencesPath & bundleId & “.plist”
tell application “System Events” to set folderName to get value of property list item “DownloadsPath” of property list file safariPrefsFile
tell current application
      set downloadFolder to (do shell script “echo “ & folderName)
    end tell
–Set Watch Folder Path
set hotFolder to current application’s NSString’s stringWithString:downloadFolder
set watcher to DirectoryWatcher’s directoryWatcherWithPath:hotFolder delegate:me
watcher’s start()
  end applicationWillFinishLaunching:
on directoryDidChange:directoryPath
set filePath to directoryPath’s stringByAppendingPathComponent:(my watchFileName)
set fileExists to (current application’s NSFileManager’s defaultManager()’s fileExistsAtPath:filePath) as boolean
log {“fileExists”, fileExists}
  end directoryDidChange:
end script

★Click Here to Open This Script