I am trying to write a script that will read a textfile with jobnumbers, find the folders and move them to another location
the textfile contains (example):
00000 p
11111 p
The first part is the beginning of a jobname and the second part tells the script what the destination is.
However, I can’t get it to work
Here’s the script and the event:
property archivedir : "/Volumes/Archief/DTP/test"
property parentdir : "/Volumes/DTP"
property archivedir_p : "/Volumes/Archief/DTP/test/permanent"
property archivedir_t : "/Volumes/Archief/DTP/test/tijdelijk"
set chosenfile to (choose file)
set filecont to read chosenfile
set archivejobs to paragraphs of filecont
repeat with archivejob in archivejobs
set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set archivejobNumber to text item 1 of archivejob
set Dest to text item 2 of archivejob
set AppleScript's text item delimiters to oldDels
end repeat
tell application "TextEdit"
activate
close every document saving no
make new document
set the name of window 1 to "Archive Info"
repeat with archivejob in archivejobs
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is "" then
set the text of the front document to "archivejobnumber " & archivejobNumber & " was not found"
else
set found to the result
if Dest contains "p" then
do shell script "mv " & found & " " & archivedir_p
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is not "" then
set failedarchive to the result
set the text of the front document to failedarchive & " was not removed after copying to " & archivedir_p
end if
if Dest contains "t" then
do shell script "mv " & found & " " & archivedir_t
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is not "" then
set failedarchive to the result
set the text of the front document to failedarchive & " was not removed after copying to " & archivedir_t
end if
end if
end if
end if
end repeat
end tell
The event output:
tell current application
choose file
alias "HD2:Users:administrator:Desktop:archivetest.txt"
read alias "HD2:Users:administrator:Desktop:archivetest.txt"
"00000 p\n11111 p"
end tell
tell application "TextEdit"
activate
close every document saving no
make new document
document "Naamloos"
set name of window 1 to "Archive Info"
do shell script "find '/Volumes/DTP' -name 11111* -type d -maxdepth 2"
"/Volumes/DTP/TESTARCHIVE/11111_TEST"
do shell script "mv /Volumes/DTP/TESTARCHIVE/11111_TEST /Volumes/Archief/DTP/test/permanent"
""
do shell script "find '/Volumes/DTP' -name 11111* -type d -maxdepth 2"
""
do shell script "find '/Volumes/DTP' -name 11111* -type d -maxdepth 2"
""
set every text of document 1 to "archivejobnumber 11111 was not found"
end tell
You have 2 repeat loops. Basically after coming out of the first repeat loop you only have the values for the last line of the text file. Nothing happens for any of the other lines prior to the last one. Basically you just need to have one repeat loop.
Second, I always try to separate commands from tell blocks when I can. For example, you have all of your “do shell script” commands inside of the tell application “TextEdit” block of code. So I separated them out. Why tell TextEdit to do a shell script? It’s just good coding practice to keep things separated because you never know when you might run into a conflict… plus it’s more efficient.
So here’s what I think your code should look like…
property archivedir : "/Volumes/Archief/DTP/test"
property parentdir : "/Volumes/DTP"
property archivedir_p : "/Volumes/Archief/DTP/test/permanent"
property archivedir_t : "/Volumes/Archief/DTP/test/tijdelijk"
set chosenfile to (choose file)
set filecont to read chosenfile
set archivejobs to paragraphs of filecont
tell application "TextEdit"
activate
close every document saving no
make new document
set the name of window 1 to "Archive Info"
end tell
repeat with archivejob in archivejobs
set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set archivejobNumber to text item 1 of archivejob
set Dest to text item 2 of archivejob
set AppleScript's text item delimiters to oldDels
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is "" then
tell application "TextEdit"
set the text of the front document to "archivejobnumber " & archivejobNumber & " was not found"
end tell
else
set found to the result
if Dest is "p" then
do shell script "mv " & found & " " & archivedir_p
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is not "" then
set failedarchive to the result
tell application "TextEdit"
set the text of the front document to failedarchive & " was not removed after copying to " & archivedir_p
end tell
end if
else if Dest is "t" then
do shell script "mv " & found & " " & archivedir_t
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is not "" then
set failedarchive to the result
tell application "TextEdit"
set the text of the front document to failedarchive & " was not removed after copying to " & archivedir_t
end tell
end if
end if
end if
end repeat
Thanks for the help.
But one problem still remains…
with
00000 p and 11111 t in the text file only 00000 p works - 00000 t was found but the "mv " was not initiated
00000 t and 11111 t both are not working - both were found but the "mv " was not initiated
00000 p and 11111 p is working
I had a small error in the code. The part with “t” was inside of the “p” if statement, so if it wasn’t “p” then it never got to the “t” code. I fixed the code above so it should work now.
Already figured it out myself.
One more thing (again)
How do I tell textedit to start writing to the next line?
do shell script ("find " & quoted form of parentdir & " -name " & archivejobNumber & "*" & " -type d -maxdepth 2")
if the result is "" then
tell application "TextEdit"
set the text of the front document to "archivejobnumber " & archivejobNumber & " was not found"
end tell
set myText to "some text"
addTextToTextEdit(myText)
on addTextToTextEdit(theText)
tell application "TextEdit"
try
set currentText to text of front document
on error
make new document
set currentText to ""
end try
if currentText is "" then
set newText to theText
else
set newText to currentText & return & theText
end if
set text of front document to newText
end tell
end addTextToTextEdit