OESF Portables Forum

Everything Else => Desktop Operating Systems Issues => Zaurus General Forums => Archived Forums => Mac Issues => Topic started by: d.maddock1 on December 22, 2005, 09:17:08 am

Title: Scripting
Post by: d.maddock1 on December 22, 2005, 09:17:08 am
While I use ZMacSync to get my PIM data on my Zaurus, I decided to write some scripts that automate some of my other common syncing tasks like copying plucker-ed news and other files, iTunes playlists, crossword puzzles, etc.  This is a work in progress, but it is already at a usable state for me; hopefully, this information will make it much easier for others to do the same.  Some of this can be used on linux as well.

First, I had to tackle the problem of automating ssh/scp actions because I have a password on my zaurus.  I use some expect scripts to copy to/from the zaurus:

Code: [Select]
zcopyto:

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set pass [lindex $argv 2]
set source [lindex $argv 3]
set dest [lindex $argv 4]
spawn sh -c "scp '$source' $user@$host:$dest"
expect "assword:" {send $pass\r}
expect eof

This script takes command line arguments and feeds them to an scp command.  Note that you should escape spaces and wildcards in filenames, if used.

Code: [Select]
zcopyfrom:

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set pass [lindex $argv 2]
set source [lindex $argv 3]
set dest [lindex $argv 4]
spawn sh -c "scp $user@$host:$source '$dest'"
expect "assword:" {send $pass\r}
expect eof

Likewise, this script copies from the zaurus to my computer.

Code: [Select]
zremovefrom:

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set pass [lindex $argv 2]
set file [lindex $argv 3]
spawn ssh $user@$host
expect "assword:" {send $pass\r}
send "rm $file\r"
send "exit\r"
expect eof  

Deletes the given file from the zaurus.

Code: [Select]
zclearinbox:

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set pass [lindex $argv 2]
spawn -noecho ssh $user@$host
expect "assword:" {send $pass\r}
send "cat /dev/null > /home/zaurus/Documents/Text_Files/inbox.txt\r"
send "exit\r"
expect eof

Finally, this expect script simply empties out a text file on the zaurus I use as an idea scratchpad.  I call this script after another script that imports the data into my todo list on my mac.


I use these scripts as glue in this large applescript that does all the syncing:

Code: [Select]
--
--
-- Settings
--
--
set the_user to "zaurus"
set the_pass to ""
set the_iP to ""

set toZDirectory to "Macintosh HD:Users:dave:Desktop:ToZ"



--
--
-- Process Zaurus Inbox File
--
--


-- Get the inbox file from the zaurus via Samba
set zInboxFile to quoted form of "/home/Main_Memory/Text_Files/inbox.txt"
set localInboxFile to "Macintosh HD:tmp:inbox.txt"


set cmd to ("cd /tmp; smbget -n -q -u " & the_user & " -p " & the_pass & " smb://" & the_iP & zInboxFile & "; echo 0" as Unicode text)
do shell script cmd

-- Read through the inbox file
tell application "Finder"
    set InboxFile to paragraphs of (read file localInboxFile)
    repeat with nextLine in InboxFile
 Â if length of nextLine is greater than 0 then
 Â     
 Â     -- Add the line to the KGTD inbox
 Â     tell application "OmniOutliner Professional"
 Â   set ThisDocument to first document
 Â   tell ThisDocument
 Â       set InboxSection to (first section whose note contains "meta_inbox")
 Â       make new row at beginning of rows of InboxSection with properties {topic:nextLine}
 Â   end tell
 Â     end tell
 Â     
 Â end if
    end repeat
    
    -- Erase temp file
    move file localInboxFile to trash
    
end tell

-- Clear the inbox on the Zaurus
set cmd to ("/Users/dave/bin/zclearinbox " & the_user & " " & the_iP & " " & the_pass)
do shell script (cmd)


--
--
-- Sync Crossword Puzzles
--
--

do shell script ("/Users/dave/bin/getpuz.sh")


--
--
-- Sync ToZ Directory
--
--

tell application "Finder"
    if (the file (toZDirectory & ":BBC News.pdb") exists) then
 Â set a to quoted form of POSIX path of (toZDirectory & ":BBC News.pdb")
 Â set cmd to ("/Users/dave/bin/zcopyto " & the_user & " " & the_iP & " " & the_pass & " " & a & " " & "/mnt/card/Documents")
 Â do shell script (cmd)
 Â 
 Â move file (toZDirectory & ":BBC News.pdb") to trash
    end if
    
    -- TODO: copy all files here into appropriate zaurus folders according to type
end tell


--
--
-- Sync iTunes Playlist
--
--

tell application "iTunes"
    set myPlaylist to playlist "ToZaurus"
    repeat with aTrack in (get every track of myPlaylist)
 Â set fileName to location of aTrack
 Â set a to quoted form of POSIX path of fileName
 Â set cmd to ("/Users/dave/bin/zcopyto " & the_user & " " & the_iP & " " & the_pass & " " & a & " " & "/mnt/card/Documents/Music_Files/")
 Â do shell script (cmd)
 Â 
 Â --TODO: remove track from the playlist
    end repeat
end tell


--
--
-- Done
--
--

display dialog ("Sync Done.")


The first section in the applescript is obvious settings.  The toZDirectory variable is a directory I use as a Mac->Zaurus outbox.  The idea is to move these files to the Zaurus; later, I intend to implement a Z->Mac one too.

The second section grabs the data from my Zaurus inbox.txt file and imports it into my task list system that uses OmniOutliner, called KGTD (http://kinkless.com/).  Each line in the inbox is an item in the outline.  Then, it clears out inbox.txt.

The next section runs a script that downloads Across Lite puzzles and copies them to the Z.  I haven't posted that one, but I can if anyone is interested.

The next section handles my Mac->Z outbox directory.  I haven't finished this section yet.  Right now, it only moves my daily BBC news file for Opie Reader.  This is generated every day via JPluck and a cron job.  If anyone is interested in a JPluck.app bundle, the data file for the BBC news, and details on setting this part up, let me know.  Ultimately, I want to move every file put in this directory to the Z, filtered into different places based on file type (Text_Files, Photo_Files, etc.).

The final section copies mp3s from an iTunes playlist to the Z.  Ultimately, it should then remove the file from the playlist.


Has anyone else attempted to put together a similar system?

Dave.