monitor a file on mac os x - macos

There are a lot of similar posts out there, but none that does exactly what I want: I want a simple script, that watches a folder on a Mac for changes and passes the filename of the modified file to command/script when changes are detected.
The tool inotifywait doesn't exist on a mac. Folder events or fswatch or watchdog seem to be getting close, but it turns out they only watch folders or only return the folder name. watchdog might be doing what I need, but I could only find commands like this
watchmedo shell-command --recursive --command='echo ${watch_src_path}' .
which again only passes the folder name in the variable watch_src_path. Does anyone know how to get it (or another program) to pass on the file name of the changed file?

Even if the question is very old, it may be useful to other people looking for the same functionality. I wrote fsw exactly to fill this gap. fsw will give you the name of the changed paths and the type of change event.
Hope this helps.
Edited: fsw was merged with fswatch.

Related

Get/Set file comment in OSX from python3?

I've searched both SE and the web, but haven't found an answer to this.
Is it possible to get/set a file comment on OSX (Sierra) in python3? I guess more accurately — is it possible to do so directly, without having to invoke mdls?
Context: I have a list of transient files that have comments on them. The files come and go, but when the come back, they'll have the same filename. I want to preserve the comments so I can re-put the comment on the files if the files are deleted and re-added. I'm going to create a python script that would output a list of file,comments, and another script that would read that list and put the comments on the associated files.
I can do everything except the actual getting/setting of the comment, and I haven't been able to find anything on if that can be done directly from python3, and if so, how.
Thanks!

Updating Batch files via Dropbox

So, there isn't anything big that I'm really going to use this for, but I've have made a little batch (.bat) program that I thought would be awesome if I had this feature in it.
I don't want anything that is going to mess up my LAN network, so try to make it as simple
as possible.
So my idea is that I want it so when the program starts it scans for an update (which will be found on Dropbox), then ask the user if they want to update, then it will install an update file and delete the old one. If anyone knows a way to do this safely, please tell me, thanks!
I have a tiny bit of an idea on how this is possible, but it is not very efficient. Thanks!
I assume you have a file on your computer and one on Dropbox and sometimes you want to edit it from another computer and then when you get back home and start up the program it will check whether the Dropbox file is more up to date than the one you have on your computer?
I think the easiest way would be to either use a special file name, like, myBatchFile_update.bat or something and then make your program check for that file in your Dropbox folder. If it's there, copy it and then rename it to myBatchFile.bat or something. Also rename update file. You may want to look up copy and xcopy and ren, first.
Another way would be to keep the "if updated" information inside of the file in a function (although it's hardly functions in batch). Or you could just keep another file called for example is_updated.bat where you keep information about whether it's updated or not. You echo something like set update=true (if it's updated) to the update file when it has updated and when you have edited the program you also edit the update file so that it says false. Then, when you need the information, you just call the update file.

tail -f all files in a directory over time

I need to monitor a directory for new files that are added (constantly: one or two every 5 seconds are added and deleted).
It would be nice to tail them constantly in terminal, but since this seems unlikely, it would be also good to append all of the tails to a file. That way when I stop my process I can review all of the files that were added and deleted (I need the contents not the filename).
A shell script will work as well as long as it will run on Mac OSX Lion.
Any help? I've tried multi-tail and tail -f (but these don't monitor the directory for new files).
Maybe xtail ?
It could be installed with homebrew or macports on osx.
CoreServices provides methods for watching changes to directories.
Apple's Carbon documentation is excellent, except that I can't seem to link to a specific function. Search for FNSubscribeByPath.
If you really need to look at the contents of the files that get added, then you can write a tool that uses FNSubscribeByPath to tell you when the directory changes and what changed, then tail -f the file.
You should probably have something that keeps track of what "tail -f" instances have been launched, so that it can kill them when the files are deleted. Otherwise, your process table will eventually fill up.
You should be able to use logstash for this. It will give you a stream of events for a set of inputs (e.g. files) and give you a stream of data you can push to another output (e.g. a file). It will also give you a lot of options for future growth, while being easy to setup in the short term.
http://www.logstash.net/

How can I set an AppleScript to run when a given file is modified?

I know that applescripts can be set to run as folder actions, but is there a way to get them to run as file actions? Yes, I know I can work around this by just making a new folder, putting the script in it, and running as a folder action, but I want to know if this can be done.
I cross-posted this question to Apple's Applescript discussion board, and a few users there gave some solutions that use only system resources, with no third-party applications. Follow the link to see the answers.
Basically you need to use launchd to watch a file and launch a script when the file changes. Then you'd need to craft a .plist that describes the script to run and the file(s) or directories to watch.
There's no simple built in way to do this. However there's lots of ways to set up an Applescript to run. However in general you'll have to do some non-Applescript coding.
Check out the info in this hint and OSXHints:
http://hints.macworld.com/article.php?story=20060817044149264
They have a Python script demonstrating using fsevents.so to monitor file changes. You would then just modify the Python to call your Applescript (or simply use Appscript to do the scripting from within Python).
Alternatively you can just have a program check a file at a particular interval and then run if the file changes. This hint describes that in Perl:
http://hints.macworld.com/article.php?story=20031001073403810
I found a way to do this that works.
Shell script:
find watchedfilepath -mtime -1 -exec cp {} watchedfolderpath ';'
Create a Calendar Event in Automator to run this shell script which will check the file. If it has been modified in the last 24 hours, then it will be copied to the watched folder. Then create an action in Automator to move it to trash (otherwise the next time the shell script runs it won't work).
Set up a Folder Action on the watched folder to watch for an added new item and then it will let you know. I have created a script to send me an email and put that in the Folder Actions folder in Library/Scripts. It works like a treat.
This is an old question, but the Apple Automator tool in MacOS does exactly this. You can set it to watch a folder and execute an Applescript, Shell Script, etc.

cocoa + skip os generated files

my app actually goes to different folders and takes each file into account and reads each file and does a lot of processing on them and marks the folder it has processed as done. but this is not happening as the system is immediately generating files like .DS_store and .localized and .trash. so is there any mechanism to skip processing hidden files or stop the os from generating these files programatically?
Thanks
Couldn't you change your app to just ignore files that start with "."? You've tagged this Cocoa, so using something like NSFileManager's contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: seems appropriate. One of the options you can specify is NSDirectoryEnumerationSkipsHiddenFiles, which will skip hidden files.
Check the documentation for more details.
I'm not aware of any option that disables the generation of .DS_Store files locally. There is an option for remote, here.
Another way to do it could be to create a unix user for just that job and let him own the dirs, so that the Finder never can go there. Either start the job manually using sudo, or make it a setuid job.. or use launchd.

Resources