Creating script files

The NightWatchman Mac client scripts are AppleScript files that require no additional functionality over and above that provided by each supported application's AppleScript interface. Script files should be created in the NightWatchman Scripts directory which resides in its Application Support folder. By default, this is:

/Library/Application Support/1E/NightWatchman/Scripts
On this page:

Naming files

To get your NightWatchman Mac client scripts to run on a NightWatchman scheduled event, you must name the scripts according to the application they support. The default script provided with NightWatchman for Macs support the saving of open text files. The name of this script is TextEdit.scpt.

The base part of the name matches the application name TextEdit exactly and is called by NightWatchman if this application is open when a scheduled event occurs. You must name your files in the same way for each application you want to support. For example, if you have an application called ACMEGraph, create a script called ACMEGraph.scpt in the Scripts folder.

Other script files

NightWatchman for Macs comes with the following script files:

Script fileDescription
AppleScript Library.scptLocated in the NightWatchman folder and provides some basic utility functions for the other NightWatchman scripts. It is a useful utility for naming and placing files and need not be modified by the user.
PreRun.scptRuns before any of the other scripts and is located in the Scripts directory.
RunAlways.scptRuns after the other scripts have run.
RunOnResume.scptRuns when the system is resuming from a low-power state.
TextEdit.scpt

Saves data in the TextEdit application during a NightWatchmanscheduled event.

This example script file has code that detects the OS. It will not run when OS X Lion is running.

Controlling scheduled events using the PreRun script

PreRun.scpt is run before the NightWatchman countdown dialog is displayed during a scheduled event and can be used to check for specific conditions or run specific actions before allowing the scheduled event to continue. To allow the scheduled event to continue, the script should return true. To stop the scheduled event the script should return false.

Controlling scheduled events using the RunAlways script

RunAlways.scpt is run after all the other NightWatchman scripts and can be used to check for specific conditions or run specific actions before allowing the scheduled event to proceed. This script is run as the last step before a user is actually logged out. To allow the scheduled event to continue the script should return true. To stop the scheduled event the script should return false.

Resuming from a low power state and the RunOnResume script

RunOnResume.scpt is run when the system resumes from a low-power state, depending on the status of the runonresume option. The success or failure of the script has no effect on the resumption of the computer. The RunOnResume script can be used to refresh network connections for running applications.

Creating a backup using the AppleScript library script

The AppleScript Library.scpt file contains a number of helpful utility functions. The three main functions which are used to save a NightWatchman backup file are:

FunctionDescription
GetExtensionOfPosixFile(posixFilePath )Returns the file extension of the file at the specified posix-formatted path.
GetTemporaryBackupFilePath()Returns the path to a temporary file to which the unsaved document should be saved.
FindAndStoreTemporaryBackupFile(originalDocumentName, originalPosixPath )Locates the temporary file and stores it as a NightWatchman backup file, using the original document name and posix-formatted path. If the unsaved file has never been saved, then empty strings should be supplied for originalDocumentName and originalPosixPath.

To use the AppleScript Library.scpt file in your script files, add the following code to each script:

set applicationSupportPath to ( path to  application support as  string )
set libraryPath to  alias  (applicationSupportPath & "1E:NightWatchman:AppleScript Library.scpt") as  string  
set library to  load script  ( alias  libraryPath)

Looking at the TextEdit script

The following listing shows the contents of the TextEdit.scpt file. The first three lines load the AppleScript Library.scpt file. The remainder of the script uses the TextEdit application's AppleScript interface to iterate through the open documents and to get the required information on them.

For each unsaved document, the script saves it to a temporary file, using the path supplied by GetTemporaryBackupFilePath, then stores the temporary file as a NightWatchman backup file, using FindAndStoreTemporaryBackupFile.

set applicationSupportPath to (path to application support as string)
set libraryPath to alias (applicationSupportPath & "1E:NightWatchman:AppleScript Library.scpt") as string
set library to load script (alias libraryPath)
tell application "TextEdit"    
   set documentList to documents    
   repeat with aDocument in documentList        
      if modified of aDocument then            
         set documentName to name of aDocument
         set originalPosixPath to ""
         set originalExtension to ""
         if (exists path of aDocument) then
            set originalPosixPath to path of aDocument
            set originalExtension to GetExtensionOfPosixFile(originalPosixPath) of library
         end if            
            set temporaryFilePath to GetTemporaryBackupFilePath() of library
         if (originalExtension ≠ "") then
            -- there is an original extension, so append it to the temporary file
            -- path, to ensure TextEdit saves the document in the same format 
            set temporaryFilePath to temporaryFilePath & "." & originalExtension
         else
            -- there is no original extension, so append ".rtf", which forces
            -- the document to be saved in rich text format
            -- otherwise, TextEdit 1.5 (10.5.8) will ignore the current format
            -- and save in plain text
            set temporaryFilePath to temporaryFilePath & ".rtf"
         end if
         close aDocument saving in temporaryFilePath            
         FindAndStoreTemporaryBackupFile(documentName, originalPosixPath) of library            
         else
            close aDocument
      end if        
   end repeat    
   quit    
end tell