Hello
This morning there was a thread starting with :
Hello MacScripter community, I just signed up for this forum and I’m an Applescript newbie, but I’m committed to doing whatever is needed to achieve my goal: to …
asking for a way to Autosave TextEdit documents every 10 minutes.
I’m unable to retrieve it.
As I built a script answering the question I post it here.
--[SCRIPT autosaveTextEdit]
(*
To autosave TextEdit documents
paste this script in the Script Editor
menu > File > Save As.
uncheck screen at startup
check "Stay Open" (* "Rester en arrière-plan" in French *)
save this script as an Application Bundle
In the Accounts PreferencePane (tab Login Items),
install it as one of the applications launched at boot_time
Every ten minutes, if TextEdit is in use,
the open documents will be duplicated,
then the duplicates are saved and closed.
Yvan KOENIG (VALLAURIS, France)
2012/02/03
*)
property minutesBetweenSaves : 10 (*
enregistrera toutes les 10 minutes *)
--=====
on idle
my autoSave()
return minutesBetweenSaves * minutes
end idle
--=====
on autoSave()
local theApp, mtFile, miDuplicate, titres, theDocs, aDoc, sourceName, leChemin, lesDocsAvant, unNom
local lesDocsApres, leDossier, nomEnListe, nomEnListe, ext, nbw, nomFenetre, nouveauNom
set theApp to "TextEdit"
set {mtFile, miDuplicate} to {3, 8}
tell application "System Events" to set titres to title of processes
if theApp is in titres then
tell application theApp
(*
Build a list of open documents *)
set theDocs to every document
repeat with aDoc in theDocs
set sourceName to name of aDoc
set leChemin to path of aDoc
try
leChemin as text
on error
(*
The doc was never saved before *)
set lesDocsAvant to name of every document
(*
Build a list of names of other open documents *)
set lesDocsAvant to {}
repeat with unNom in lesDocsAvant
if unNom is not (aDoc as text) then set end of lesDocsAvant to unNom
end repeat
(*
Save the doc; You will be asked to define a name and a location *)
save aDoc
(*
Loop extracting the name of the saved document *)
set lesDocsApres to name of every document
repeat with unNom in lesDocsApres
if unNom is not in lesDocsAvant then exit repeat
end repeat
(*
Get container and name of the newly saved document *)
set leChemin to path of document unNom
set sourceName to unNom
end try
(*
Now, we are working on a document which was saved at least once so a pathname is defined.
We may get the path to it's container. *)
tell application "System Events"
set leDossier to path of container of disk item leChemin
end tell
(*
Extract name extension and name without extension *)
set nomEnListe to my decoupe(sourceName, ".")
set nomCourt to my recolle(items 1 thru -2 of nomEnListe, ".")
set ext to item -1 of nomEnListe
tell application theApp to activate
tell application "System Events" to tell process theApp
set nbw to (count of (every window whose subrole is "AXStandardWindow"))
(*
Trigger the menu item Duplicate *)
tell menu bar 1 to tell menu bar item mtFile to tell menu 1 to click menu item miDuplicate
(*
Wait for possible sheet "document was edited, are you really wanting to duplicate it"
and click "Duplicate" if it appears *)
repeat 5 times
delay 0.1
if exists first sheet of window sourceName then
click first button of first sheet of window sourceName
exit repeat
end if
end repeat
(*
Wait for the duplicate window *)
repeat
delay 0.1
if nbw < (count of (every window whose subrole is "AXStandardWindow")) then exit repeat
end repeat
(*
The duplicate window is available *)
set nomFenetre to (name of first window whose subrole is "AXStandardWindow")
(*
Build a date_time_stamped file name *)
set nouveauNom to nomCourt & my buildstamp() & ext
(*
Save the duplicate with the date_time_stamped name *)
my saveIt(nomFenetre, nouveauNom, leDossier)
(*
Close the duplicate *)
tell window nouveauNom
click button 1
end tell
end tell -- System Events.
end repeat -- aDoc
end tell -- theApp
end if -- theApp.
end autoSave
--=====
on saveIt(dName, nName, dDossier)
tell application "System Events"
make new file at end of folder dDossier with properties {name:nName}
end tell
set dAlias to (dDossier & nName) as alias
tell application "TextEdit"
save document dName in dAlias
end tell
end saveIt
--=====
on buildstamp()
return do shell script "date +_%Y-%m-%dT%H.%M.%S."
end buildstamp
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, t
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
--[SCRIPT]
Yvan KOENIG (VALLAURIS, France) vendredi 3 février 2012 15:01:13