Wednesday, June 23, 2010

'Move' with automator

Automator : Create automations without programming

As simple as it might be, yes automator is a over looked, under estimated tool available in Mac OS X. 
Any one with no programming knowledge too can with point-and-click knowledge can create workflows to achieve almost anything. Although personally, I'd recommend it as a great tool for repetitive tasks such as renaming, etc! Or some task that's not repetitive in a single stretch but something that you might perform now and then like backing up your data, etc.

Here are the basics. You create a workflow that's more like flow chart. Top to bottom, the actions that you define are executed, with one's output being sent to the next on the line.

Actions are categorized on the left most (library) pane, shown in the screenshot below. The actions under the selected category are in the pane to the right of it.

Nothing gets easier than using an example: the one that I quoted in my previous post: Moving files under multiple folders into a single folder.


In our scenario, the folder named 'Jdownloads' contains 11 folders named as

Ubisoft E3 2010 Conference [Part 1 of 11] - Child of Eden(HQ)
...
Ubisoft E3 2010 Conference [Part 11 of 11] - Michael Jackson The Game

The contents of these must be moved to 'E3 Conference'. A mundane task that I hate to do with all those mouse clicks or key board hits (I mostly use the keyboard). Whichever way, I HATE to do this!

So define the 3 actions shown in Fig. 1. 
  1. Find Finder Items     ---- (A1)
  2. Get Folder Contents ---- (A2)
  3. Move Finder Items   ---- (A3)
A1:
This action can be customized with the search options it provides. It under the hood just uses the 'search' Mac provides. So am gonna just search for the 11 folders. How do I do that? 

Look under the Jdownloads folder, for 'folders' that contain the text 'e3' in the name. The action and it's results on single-stepping are shown below. Don't be flabbergasted by the result: 12 folders. The extra 12th is the output folder. No harm done. It's empty anyway :)



A2:
Iterate through all the folders of the previous result and get their contents. In this case, 1 file in each foler adding up to 11 files.



A3:
Move the items to the specified folder!

As simple as that. Now, I could've also deleted the source folders after the move. But why don't you just try that, hands-on!


Thursday, June 17, 2010

Automator to the rescue

    The long awaited E3 conference is here and I couldn't wait to watch Ubisoft's stall & developers staging its latest mind-blowing games! Wow! Assassin's creed bloodlines is going to awesome & a new Michael Jackson game for fans is rolling out soon! (For those of you wondering what this E3 conf is... it's all about games - Hardware & software. Why don't you follow the wiki link or E3's link?)

Jdownloader helped me persist the youtube videos in my HDD to watch later. The low quality videos (all except AC Brotherhood in HD ;-)) were downloaded, thanks to JD. Problem popped up when each single video was individually downloaded into a separate folder. Now I have to move every little file into some folder, say 'E3 Conference'. Or..... why not use automator of Mac OS X!

For newbies, Automator is an app that can be used to automate your jobs. (Now, Cool down! I know that didn't help! ;-)) A simple example: Remember that hundreds of pics that you captured on a tour and wanted to rename them one by one. It must've been exhausting... You thought of completing it later. Only that later never came.  Things like that can be done easily.

Our problem definition again: 11 videos. 11 separate files in 11 different folders. I hate too much branching (folders here).

Expected results: 11 videos in 1 folder.

You know the usual solution of course(Drag, drop, delete)! Now to the automator kinda solution!

To give you a clear picture: Here's how the folder looks before running the automator workflow.



And here's how it looks after moving them!




Here's the tiny workflow with just 3 actions that achieved this!


If that doesn't make sense, no worries! The following post will clear the fog!

Wednesday, June 2, 2010

Python: Module search path



When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python


Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation- dependent default.

Now what if you have not specified PYTHONPATH during installation? How do I edit it?

Suppose, I have my module spam.py in ~/geekcode/python. I have my other program user.py that needs to the aforementioned module.

This module search path is comparable to CLASSPATH in java.

To add ~/geekcode/python to PYTHONPATH, in your interactive mode of the Python interpreter, edit the path variable in the sys module.

>>> import sys
>>> sys.path

Printing sys.path as shown above will show up your current PYTHONPATH.

This sys.path is just another mutable python list. Hence you can easily edit it using the usual list methods, here, the append method.

sys.path.append("/Users//geekcode/python")

The only drawback of this method is that, this setting is forgotten once you restart the interpreter. 

To persist this setting, you can use the '.pth' file.  A '.pth' file with any name like 'foobar.pth' can contain any number of newline separated list of paths you need to add to PYTHONPATH aka path aka sys.path variable. It is similar to .bash_profile or .profile files of Unix systems. The paths in .pth files are added automatically to the PYTHONPATH variable whenever the interpreter restarts.

All said, now the question is, "Where do I place this .pth file?". It can be placed in any path that is already mentioned in sys.path! EASY!