Changes

Jump to navigation Jump to search
→‎AppleScript: revised with error code; added script to zip Scrivener projects before unison sync
Line 67: Line 67:  
To avoid opening the terminal window, create the following AppleScript and save as an application.
 
To avoid opening the terminal window, create the following AppleScript and save as an application.
   −
set resultsText to do shell script "unison /Users/houpt/uniDisk ssh://houptlab.org/uniDisk -ui text -batch -times -terse -ignore 'Name .DS_Store'"
+
      try
 +
            -- add "-terse" if you want less verbose results
 +
            set resultsText to do shell script "unison /Users/houpt/uniDisk ssh://houptlab.org/uniDisk -ui text -batch -times -ignore 'Name .DS_Store'"
 +
           
 +
        on error errStr number errorNumber
 +
           
 +
            set unisonResultCodeString to (errStr & return & return & "Result: number " & errorNumber & return & return)
 +
           
 +
            if (errorNumber is equal to 0) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Successful synchronization; everything is up-to-date now")
 +
           
 +
            else if (errorNumber is equal to 1) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Some files were skipped, but all file transfers were successful.")
 +
           
 +
            else if (errorNumber is equal to 2) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Non-fatal failures occurred during file transfer.")
 +
           
 +
            else if (errorNumber is equal to 3) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "A fatal error occurred, or the execution was interrupted.")
 +
           
 +
            else
 +
           
 +
                set resultsText to unisonResultCodeString
 +
           
 +
            end if
 +
           
 +
           
 +
        end try
 
   
 
   
  -- display the results text in a new TextEdit window
+
   
-- note that if "-terse" is set, then unison only returns a result if there are conflicts
+
        display alert "Unison completed sync" giving up after 2
tell application "TextEdit"
+
activate
+
        -- display the results text in a new TextEdit window
set NewDoc to make new document
+
        tell application "TextEdit"
if (0 is length of resultsText) then
+
            activate
set text of NewDoc to (date string of (current date)) & (time string of (current date)) & return & return & "Unison reports no conflicts."
+
            set NewDoc to make new document
else
+
            if (0 is length of resultsText) then
set text of NewDoc to (date string of (current date)) & (time string of (current date)) & return & return & resultsText
+
                set text of NewDoc to (date string of (current date)) & " " & (time string of (current date)) & return & return & "Unison reports no conflicts."
end if
+
                else
end tell
+
                set text of NewDoc to (date string of (current date)) & " " &(time string of (current date)) & return & return & resultsText
 +
            end if
 +
        end tell
 
   
 
   
 
  tell me to quit
 
  tell me to quit
 
   
 
   
   −
Results of the synch will be silent, but will be stored in the "unison.log" file. If there are conflicts to be resolved, then the applescript will post a TextEdit window.
+
Results of the synch will be stored in the "unison.log" file, and the applescript will display results in a TextEdit window.
 +
 
 +
===Unison With Scrivener===
 +
 
 +
Scrivener stores its projects as bundles (i.e. folders). Because Unison does not sync modification times of folders, changes to Scrivener projects do not get properly propagated. To get around this, I use this more complicated AppleScript to zip up any Scrivener projects -- the zipped file gets propagated properly, with the Scrivener project modification times properly preserved and transmitted. Needless to say, this is a hack; but I really want to sync my Scrivener projects on multiple machines!
 +
 
 +
--
 +
--  AppDelegate.applescript
 +
--  SyncUniDisk
 +
--
 +
--  Created by Tom Houpt on 12/12/28.
 +
--  Copyright (c) 2012 Tom Houpt. All rights reserved.
 +
--
 +
 +
  script AppDelegate
 +
        property parent : class "NSObject"
 +
 +
on applicationWillFinishLaunching_(aNotification)
 +
-- Insert code here to initialize your application before any files are opened
 +
       
 +
        -- to handle incompatibilities of Unison file-synching with Scrivener's use of bundles for files
 +
        -- T.A. Houpt, 2014-1-22
 +
        --
 +
        -- find all Scrivener "*.scriv" files (actually bundles) and zip them up
 +
        -- then rename original file as "*.scriv YYYYMMDD hhmmss"
 +
        -- then call unison in batch mode
 +
        -- post an alert that we finished syncing
 +
        -- display the unison results in a TextEdit window
 +
        -- (we could unzip any *.scriv.zip files, but we do the unzipping manually for the moment)
 +
 
 +
  try
 +
 
 +
        set allScrivenerFileNames to do shell script "find '/Users/houpt/uniDisk' -name '*.scriv' -print"
 +
        set AppleScript's text item delimiters to return
 +
        set scrivenerFileRecords to text items of allScrivenerFileNames
 +
       
 +
        -- zip all the .scriv files
 +
        -- for each .scriv file we found:
 +
        --  1) delete previous zip file if it exists
 +
        --  2) zip the .scriv file
 +
        --  3) if we created the zip file, rename the "*.scriv" file as "*.scriv YYYYMMDD hhmmss"
 +
       
 +
        repeat with aRecord in scrivenerFileRecords
 +
            if length of aRecord is greater than 0 then
 +
               
 +
                set scrivenerFile to aRecord
 +
                set filePath to quoted form of scrivenerFile
 +
                set zipFile to quoted form of (scrivenerFile & ".zip")
 +
               
 +
                -- if there is a previous zip file, then delete it
 +
                set existsResults to do shell script "[ -e " & zipFile & " ] && echo 'Found' || echo 'Not found'"
 +
               
 +
                if (existsResults is "Found") then
 +
                    do shell script "rm -f " & filePath
 +
                end if
 +
               
 +
                do shell script "zip -r  " & zipFile & " " & filePath
 +
               
 +
                -- if the file was zipped, then rename the original file with date & time
 +
                set existsResults to do shell script "[ -e " & zipFile & " ] && echo 'Found' || echo 'Not found'"
 +
               
 +
                if (existsResults is "Found") then
 +
                    set {year:y, month:m, day:d, hours:h, minutes:mi, seconds:s} to (current date)
 +
                    set dateNumber to y * 10000 + m * 100 + d
 +
                    set timeNumber to h * 10000 + mi * 100 + s
 +
                   
 +
                    set newFilePath to quoted form of (scrivenerFile & " " & dateNumber & " " & timeNumber)
 +
                   
 +
                    do shell script "mv " & filePath & " " & newFilePath
 +
                end if
 +
            end if
 +
        end repeat
 +
       
 +
        on error errStr number errorNumber
 +
 
 +
            set zipErrorString to ("Zip Error: " & errStr & " number " & errorNumber)
 +
           
 +
            display alert zipErrorString giving up after 10
 +
 
 +
        end try
 +
 
 +
 
 +
        try
 +
            -- -terse
 +
            set resultsText to do shell script "unison /Users/houpt/uniDisk ssh://houptlab.org/uniDisk -ui text -batch -times  -ignore 'Name .DS_Store'"
 +
           
 +
        on error errStr number errorNumber
 +
           
 +
            set unisonResultCodeString to (errStr & return & return & "Result: number " & errorNumber & return & return)
 +
           
 +
            if (errorNumber is equal to 0) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Successful synchronization; everything is up-to-date now")
 +
           
 +
            else if (errorNumber is equal to 1) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Some files were skipped, but all file transfers were successful.")
 +
           
 +
            else if (errorNumber is equal to 2) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "Non-fatal failures occurred during file transfer.")
 +
           
 +
            else if (errorNumber is equal to 3) then
 +
           
 +
                set resultsText to (unisonResultCodeString & "A fatal error occurred, or the execution was interrupted.")
 +
           
 +
            else
 +
           
 +
                set resultsText to unisonResultCodeString
 +
           
 +
            end if
 +
           
 +
           
 +
        end try
 +
 
 +
 
 +
        display alert "Unison completed sync" giving up after 2
 +
       
 +
        -- display the results text in a new TextEdit window
 +
        tell application "TextEdit"
 +
            activate
 +
            set NewDoc to make new document
 +
            if (0 is length of resultsText) then
 +
                set text of NewDoc to (date string of (current date)) & " " & (time string of (current date)) & return & return & "Unison reports no conflicts."
 +
                else
 +
                set text of NewDoc to (date string of (current date)) & " " &(time string of (current date)) & return & return & resultsText
 +
            end if
 +
        end tell
 +
       
 +
       
 +
        tell me to quit
 +
 
 +
    end applicationWillFinishLaunching_
 +
 
 +
    on applicationShouldTerminate_(sender)
 +
          -- Insert code here to do any housekeeping before your application quits
 +
        return current application's NSTerminateNow
 +
    end applicationShouldTerminate_
 +
 
 +
end script
    
==Conflicts==
 
==Conflicts==

Navigation menu