Hey guys, quick question…
What’s the functional difference in between using “on idle” with a return 10 at the end, vs a normal loop with a delay 10 at the end?
I’m considering writing a script that constantly checks if VmWare Fusion is running, so that when it closes, I’m asked if I want to backup the virtual machine file (using an awesome script I put together!! I’ll post it right after this). I figure I need to make an application in automator that calls a script that loads vmware fusion. Then when it finds that fusion isn’t running anymore, it’ll bug me to do a backup if I want.
So I’m curious, on idle vs loop with a delay…
Any reason to use one instead of the other?
Could I please have an example of 1 or 2 situations in which I’d want to use one instead of the other?
here’s the vm backup script.
You’d have to change the location to backup to, and the name of the virtual machine file.
If I had more than one virtual machine, then I’d set it to copy the whole virtual machine folder, but I’m not cool enough for that!
first, these handlers are in a script file called finderLib.scpt :
on checkIfMounted(volumeName)
tell application "Finder"
set mountedDisks to list disks
if mountedDisks contains volumeName then
return true
else
display dialog "Backup disk is not mounted" with icon stop buttons "Quit" default button "Quit"
if button returned of result = "Quit" then
return false
else
return false
end if
end if
end tell
end checkIfMounted
on checkFreeSpace(volumeName)
tell application "Finder"
return free space of alias volumeName
end tell
end checkFreeSpace
on checkFileSize(file_)
tell application "Finder"
set size_ to size of (info for file_)
return size_
end tell
end checkFileSize
on findOldest(folder_)
tell application "Finder"
set list_ to every item of folder_
set oldest_ to item 1 of list_
repeat with file_ in list_
if modification date of (oldest_) > modification date of (file_) then
set oldest_ to file_
end if
end repeat
end tell
return oldest_ as alias
end findOldest
on sendToTrash(file_)
tell application "Finder" to move file_ to trash
end sendToTrash
on emptyTrash()
tell application "Finder"
set trash_ to every item of trash
if (count of trash_) < 1 then
return "Already Empty"
else
empty trash
return "Emptied Trash"
end if
end tell
end emptyTrash
on duplicateFileToFolder(file_, folder_)
tell application "Finder" to set file_copy to duplicate file_ to folder_ with replacing
return file_copy as alias
end duplicateFileToFolder
on makeTimeStamp()
set dateTime to current date
set totalSeconds to (time of (dateTime))
set hour_ to totalSeconds div 3600
set minutes_ to (totalSeconds mod 3600) div 60
if minutes_ < 10 then
set minutes_ to "0" & minutes_ as string
end if
set seconds_ to totalSeconds mod 60
if seconds_ < 10 then
set seconds_ to "0" & seconds_ as string
end if
set time_ to (hour_ as string) & minutes_ & seconds_
set date_ to short date string of dateTime
set as_txt_del to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set day_ to day of dateTime as number
set year_ to year of dateTime
if day_ < 10 then
set day_ to "0" & day_ as string
end if
set month_ to month of dateTime as number
if month_ < 10 then
set month_ to "0" & month_ as string
end if
set date_ to year_ & month_ & day_
set AppleScript's text item delimiters to as_txt_del
set time_stamp to date_ & "_" & time_
return time_stamp as string
end makeTimeStamp
on appendName(file_, appendage_)
tell application "Finder"
set name_ to displayed name of (info for file_)
set extension_ to name extension of (info for file_)
set name_ to (name_ & " " & appendage_ & "." & extension_) as string
set name of file_ to name_
end tell
end appendName
Then, here’s the main script (which I call from an applescript thing in automator) :
on backupVM()
set path_ to path to scripts folder
set finderLib to (path_ as string) & "finderLib.scpt" as text
set finderLib to (load script file finderLib)
set docPath to path to documents folder
set vm_ to (docPath as string) & "Virtual Machines.localized:Windows 7.vmwarevm" as alias
set backupDisk to "vmBackup"
tell finderLib
if checkIfMounted(backupDisk) = false then
return "Backup disc not mounted"
end if
set enoughRoom to false
repeat until enoughRoom = true
if checkFreeSpace(backupDisk) ≤ checkFileSize(vm_) then
set enoughRoom to false
set file_ to findOldest(backupDisk)
sendToTrash(file_)
emptyTrash()
else
set enoughRoom to true
end if
end repeat
with timeout of 7200 seconds
set vm_backup to duplicateFileToFolder(vm_, backupDisk)
end timeout
set time_stamp to makeTimeStamp()
appendName(vm_backup, time_stamp)
end tell
end backupVM
Hi.
You’d normally use ‘delay’ to make a script pause for a short while before going on to the next command. This pause is part of the execution of the script.
For constantly repeated periodic checks, a stay-open script with an ‘idle’ handler is better. It does its thing and then sits there idle (ie. not executing) until the system gives it a nudge after the specified time. This frees up resources from running the script and allows the computer to get on with (more) other things in the meantime.