Hostinicon
GET HOSTING FROM $3.95/MO PLUS A FREE YEAR ON TUTS+ (RRP $180). HURRY OFFER LIMITED. Check it out
Advertisement

Track Project Time With Alfred Timekeeper

by
Gift

Get a free year on Tuts+ this month when you purchase a Siteground hosting plan from $3.95/mo

Final product image
What You'll Be Creating

Alfred Time Keeper

Many people juggle multiple projects with different time schedules. From web apps to dedicated programs, there’s already a number of ways to keep track of time spent on a project. Most require net service and/or a program to stay running all the time. Often this isn’t practical.

Alfred Timekeeper works similar to a punch card time keeping system: you save a timestamp when you start and stop working. Since it just records timestamps, a program or web service isn’t always open.

In this tutorial, I'll assume that you’re already familiar with writing workflows in Alfred. If not, please check out these tutorials: Alfred for Beginners, Intermediates, Advanced users and Alfred Debugging.

Overall Design

Many people work with different computers in different locations. That makes keeping track of projects and time management that much harder. By using Dropbox, all of the project and time management information is synchronized automatically.

This approach necessitates the keeping of all information in files and not in computer memory. This restriction requires the use of two data storage arrangements: local directory of information used by that system’s program only, and a synchronized directory that’s shared with every computer connected to it.

The local storage contains the reference to the synchronized directory and the editor to edit timesheets. These files are in Alfred’s data directory location.

The synchronized directory keeps all the timesheets, lists of projects, last known state of recording information, and the timezone information. That will be in a Dropbox location. Any file system synchronization system can be used, I just happen to have Dropbox already.

Creating the Workflow

In Alfred, create a new workflow called Alfred Timekeeper.

Creating the Workflow
Creating the Workflow

In this new workflow, you need to create a File Filter that will only list directories.

Set Timesheet Directory Command
Set Timesheet Directory Command

You can set the File Types by dragging a directory from Finder to the File Types area. Set the Search Scope to the directory that contains your DropBox account.

Add a Run Script block after this block, unselecting all the escaping options. To that block, add this script:

This script checks for the data directory to be there. If not there, it creates the directory and the files used for the workflow. It takes the input in the {query} macro and places it in the dir.txt file in the data directory. It then notifies the user of the action.

Connect the Run Script block to a notification block. All Run Script blocks from here on should connect to this notification block. Set its output to be the {query} macro and set the Only show if passed in argument has content.

Once saved, set the timesheet directory. Inside the Dropbox directory, create a directory for timesheets and use the atk:setdir command to set that directory. You’re now running a multiple system timekeeping program!

That's the basic setup routine for the workflow. Before tackling the main program, you need to setup the working environment.

Setting Up the Go Environment

The easiest way to install the go programming language is by Homebrew. If you haven’t installed Homebrew yet, the tutorial Homebrew Demystified: OS X’s Ultimate Package Manager will show you how.

In a terminal, type:

In the home directory, create the directory go. The go language will store all downloaded libraries there. Add to the .bashrc file and/or .zshrc file this line:

If you’re using fish, add this to the config.fish file:

After reloading shell, you can install the goAlfred library by typing:

This library makes using the go language with Alfred much easier. It will create the directories to the data storage area for Alfred workflows and make the XML listing needed in Script Filters.

Timekeeper.go

To create the main program file, you have to go to the directory for the workflow. Open the Run Script block.

Opening the Workflow Directory
Opening the Workflow Directory

Click the Open workflow folder button at the bottom of the Script: area. This opens the workflow directory in Finder (or Pathfinder if you have it). To open the directory in a terminal shell, you can use the Alfred Workflow TerminalFinder to open a Finder or Path Finder directory in a terminal session (or iTerm).

Creating the Program File in Terminal
Creating the Program File in Terminal

Once in the terminal program, type:

Open that file in the editor of choice. Once open, put this code in:

That code defines all the functionality of the Alfred Timekeeper. The design of the program is to do different functions based on a command letter and options parameters after it. It will get the timesheet directory from Alfred and create the project files and timesheets as needed.

Compiling

You have to compile the program before you can use it. The go language isn’t an interpreted language, but a compiled language. This allows the program to run much faster.

In the terminal program, type:

Compiling Timekeepergo
Compiling Timekeeper.go

If everything is in place properly, you should now have the Timekeeper program in that directory. It is in red above. If something did not get copied right and the compile fails, lookup the line number given and compare it to the above. Or, get a fresh copy from the download given with this tutorial.

Adding Alfred Functions

With the program finished, you need to call it from Alfred. Create a Keyword block with the keyword set to atk:addproject. Connect it to a Run Script block and put this line of code:

All Run Scripts blocks here on out should be set to run bash scripts and none of the escaping options set. The above script allows for creating new projects. Use this to create your different projects. I have the projects: Envato, CustomCT, and Missions created for the work I do.

Now that you have projects, you now need to set one as the current project. Create a Script Filter as below:

Script Filter for Selecting a Project
Script Filter for Selecting a Project

Add a Run Script block after it with the following script:

Setting the Project
Setting the Project

Now, you can set the project you want to work on. In the Alfred Prompt, type atk:project and a list of the created projects should appear. Select the project you want and it will automatically start timing for you.

Therefore, if you selected the Tutorials project, there is a file named Tutorials_2014–05–05.txt (that is, as long as you created it on May 5, 2014). Inside that file is a time stamp, a colon, and a start statement. You will also have the project.txt file with the currently selected project, laststate.txt file with the last state (ie: start or stop), and the projects.txt file with a list of created projects.

The timing has started, but now you need to stop it. Create a new Script Filter block with the keyword set to atk:state and the script set to:

That should connect to a Run Script block with the script set to:

The s command tells Timekeeper program to generate an XML output for the next state. It will automatically have the first option in the XML to be the opposite of the current state.

Changing Timing States
Changing Timing States

You can now toggle the state of timing a project with atk:state. You can also create hotkeys to start and stop the timing. Try to do that one on your own. You never learn until you try!

Adding an External Trigger
Adding an External Trigger

You might want an external program to set the state of time recording. Therefore, create a External Trigger block as shown. Connect this block to the previous Run Script block.

Adding an External Trigger - Configuration
Adding an External Trigger - Configuration

The Sample Code at the bottom can be ran by any AppleScript aware program to trigger start/stop actions on the current project. If you replace the text test with stop, it will stop timing. If you replace the text test with start, it will start timing. Used with ControlPlane, you can create a rule to turn off timing when the computer sleeps and resume timing when the computer wakes.

Now that you’re starting and stopping the time, you need to view the time spent. Create a Keyword block with atk:current. Connect it to a Run Script block with this script:

Connect it to the Notification block.

Current Time Command
Current Time Command

When you run atk:current in Alfred prompt, you will get the current time spent on the project and the current state.

Second Computer

If you have another computer, setup Alfred to share over Dropbox. Once set up, all of your workflows will automatically update from one computer to the other.

Setting Up Alfred Syncing
Setting Up Alfred Syncing

Select the Set sync folder to set up syncing via Dropbox and follow the directions. On the second computer, use the command atk:setdir to set the timesheet directory you created in Dropbox. The two systems can now start and stop project timings. Make sure Dropbox is fully synced in between state changes.

Conclusion

That’s the basics of the Alfred Timekeeper. The workflow packaged with the download is the full version with other features. You now understand how to create your own time management system that’s scalable to use on multiple computers using Dropbox and does not take up computer resources. 

Since I use this everyday, I will be adding more features such as a web interface to see the timesheets graphically. If you can come up with other features, try to add them yourself! That’s the fun of working with Alfred workflows.

Advertisement