Tuesday, December 18, 2012

Applescript to make DayOne entries from a folder with images

Today, I made an Applescript to make DayOne entries from a folder with images. It uses EXIFtool to extract date and time from the meta information stored inside the image. The code was heavily borrowed from this posting on making DayOne entries from Aperture. Somehow, that script did not work for some images.
If you want the image file to be deleted, uncomment the line at the end of the script

## start of code ##

# Create Dayone entries from folder with JPEGs
# based on http://d.pr/n/a2MQ

# Can only be used for http://dayoneapp.com
# in combination with the associated CLI tools
 
# change the path to your export folder
set a_Folder to "/Users/username/Desktop/export/"

set a_folder_list to list folder a_Folder without invisibles
repeat with x from 1 to count of a_folder_list
    set the_file to quoted form of (a_Folder & item x of a_folder_list)
    set shell_command to "exiftool -exif:DateTimeOriginal " & the_file
    #display dialog shell_command
    set a_date to do shell script shell_command
    set theYear to word 4 of a_date as integer
    set theMonth to word 5 of a_date
    set theDay to word 6 of a_date
    set theHour to word 7 of a_date
    set theMin to word 8 of a_date
    set theSec to word 9 of a_date
    set theDate to theMonth & "/" & theDay & "/" & theYear as string
    #display dialog theMin
    set theAP to "AM"
    if theMin is less than 10 then
        set theMin to "0" & theMin
    end if
    if theHour is greater than 12 then
        set theAP to "PM"
        set theHour to theHour - 12
    end if
    set theTime to theHour & ":" & theMin & " " & theAP as string
    set theDateTime to "\"" & theDate & " " & theTime & "\""
   
    set theScript to "echo \"\" | /usr/local/bin/dayone -p=" & the_File & " -d=" & theDateTime & " new" as string
   
    set theEntry to do shell script theScript
    set theLength to the length of theEntry
    set theStart to theLength - 39
    set theEnd to theLength - 8
    set theUDID to text theStart thru theEnd of theEntry
   
    tell application "Safari"
        set the URL of the front document to "dayone://edit?entryId=" & theUDID
    end tell


#    uncomment the line below to enable deletion of the image file
#    do shell script "rm " & the_File
   
end repeat


## end of code ##

Saturday, December 18, 2010

Low on power

Okay, there is this quiet little Intel D510MO computer running in the corner. It consumes 20W at peak. It is not doing anything else than taking care of the Time Machine backups of my MacBook. And that is the only client.

When I sleep, the MacBook sleeps so keeping this backup server running all night or during office hours does not make sense. Luckily, it runs Linux so anything should be possible. And yes, someone did the actual thinking for me! And in case, that page ever goes down, the script is attached below. I made a small modification: I am letting it go to hibernate instead of sleeping. A cronjob runs this script every hour, so within an hour after I go to bed, the server hibernates. This runs perfectly well. The machine is up and running in 10 or 20 seconds. How to wake it up? WakeOnLan. In the BIOS, I had to enable that option and that was it.

On the Mac, I installed the program wakeonlan via Darwinports. And also the Dashboard widget WakeOnLan version 0.89 for testing. The server wakes up using the widget! And also from the command line: `/opt/local/bin/wakeonlan "xx:xx:xx:xx:xx:xx"' (xx'es denote the MAC address of the server). Next is setting up a cronjob on the Mac to wake up the server every 15 minutes.  Using Cronnix, setting this up was easy. I added a new line, "*/15 * * * *" executing `/opt/local/bin/wakeonlan "xx:xx:xx:xx:xx:xx"' every 15 minutes. It's that easy. The cronjob is automatically executing tasks; every 15 minutes a wakeonlan signal is sent to the server. If it is asleep, it wakes up. If it is alive or out of reach, the command dies.

Conclusion: an energy efficient server can be made even more efficient by shutting it down when not in use and waking it up when needen.

Attachment: hibernate script on server.
#!/bin/bash
#
# This is scheduled in CRON.  It will run every 20 minutes
# and check for inactivity.  It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
# by : vashwood ( http://ubuntuforums.org/showthread.php?t=530973 )
# Changed by CYBT for Fedora 27 July 2008
#

log=/root/power/log

# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`

#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log

# Check if RX/TX Files Exist
if [ -f /root/power/rx ] || [ -f /root/power/tx ]; then
p_rx=`cat /root/power/rx`  ## store previous rx value in p_rx
p_tx=`cat /root/power/tx`  ## store previous tx value in p_tx

echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log

echo $rx > /root/power/rx    ## Write packets to RX file
echo $tx > /root/power/tx    ## Write packets to TX file

# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`

echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
#echo "t_rx: "$t_rx
#echo "t_tx: "$t_tx
#echo "rx: "$rx
#echo "tx: "$tx
echo " " >> $log

if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then  ## If network packets have not changed that much
echo "Suspend to Ram ..." >> $log
echo " " >> $log
rm /root/power/rx
rm /root/power/tx

sudo /usr/sbin/hibernate   ## Force Sleep
fi

#Check if RX/TX Files Doesn't Exist
else
echo $rx > /root/power/rx ## Write packets to file
echo $tx > /root/power/tx
echo " " >> $log
fi

Friday, December 17, 2010

Shuttle NAS died - is now Atomized

Back in 2008, I purchased a Shuttle K45 barebone, added a processor, 1GB RAM and 2x 750GB. I also exchanged the stock PSU for a quiet, fanless version, the PC62. Aim: a quiet Time Machine backup server running on Linux. Halfway 2010, the computer started to make funny noise, unpredictable behavior and refused to reboot in the end. Dead.

Long live the Internet: I was not alone. So, I tried to fix it. Ordered capacitors on Ebay, got myself a soldering iron and checked out Youtube. Again, I was not the first one to replace items on a motherboard. And probably also not the first one to mess it up: It booted once and failed misserably after an hour or so. I did try again but no luck (or skills).

I ended up purchasing a new mother board: the Intel D510MO, a fanless mother board with a dual core Atom D510 processor. How cute! Without too much effort, I was able to place it in the Shuttle K45 case. It is now happily running Debian Squeeze and accepting Time Machine backups. I have no issues with speed in normal day to day life; compiling a new kernel however takes a lot of time.

Why Squeeze (Debian Testing at the time of writing)? Debian Testing is not considered the wisest choice for a server. But it ships with a newer version of netatalk; capable of talking with Time Machine out of the box.

Conclusion: Shuttle K45 is suffering from bad capacitors which can be replaced by a skilled person. If you mess it up, the microATX Intel D510MO main board is a good replacement.

Next up: power saving