Hi, I have a file on my desktop called codes.rtf which is a CR delineated list of codes.
All I want to do is read and return the first code, then delete it along with its carriage return.
I’ve been searching for ages and i’ll continue to but this is for tomorrow so i’m panicking!!! Please help!
The idea is that each code is unique and once its been used on a PDF (in automator) it is deleted from the list.
Hi,
is the RTF text format required?
It’s pretty easy to do it with plain text
No, i’ve changed to txt and have managed to read the first line, now I’m just trying to delete the first line…
set theFile to ((path to desktop folder) & "codes.txt") as string
set theFileReference to open for access theFile
set theFileContents to read theFileReference until return
return paragraph 1 of theFileContents
close access theFileReference
try this, the variable currentCode contains the code of the first line.
The rest is written back to disk
set codesFile to (path to desktop as text) & "codes.txt"
try
set fileRef to open for access file codesFile with write permission
set codes to paragraphs of (read fileRef)
set numberOfCodes to count codes
set currentCode to item 1 of codes
if numberOfCodes > 1 then
set restCodes to rest of codes
set {TID, text item delimiters} to {text item delimiters, return}
set restCodesText to restCodes as text
set text item delimiters to TID
else
set restCodesText to ""
end if
set eof fileRef to 0
write restCodesText to fileRef
close access fileRef
display dialog currentCode
on error errorMessage number errorNumber
if errorNumber = -39 then
display dialog "No codes available" buttons {"OK"} default button 1
else
display dialog errorMessage buttons {"OK"} default button 1
end if
try
close access file codesFile
end try
end try
Absolute hero, Its much more than I thought. Thanks for putting the feedback and error checking in but its blocking the automator action because it needs confirmation by pressing OK. I’m experimenting with it, but could you post a stripped version?
to remove the confirmation just cut the first display dialog line
This is an AppleScript action version, the return value is the requested code
on run {input, parameters}
set codesFile to (path to desktop as text) & "codes.txt"
try
set fileRef to open for access file codesFile with write permission
set codes to paragraphs of (read fileRef)
set numberOfCodes to count codes
set currentCode to item 1 of codes
if numberOfCodes > 1 then
set restCodes to rest of codes
set {TID, text item delimiters} to {text item delimiters, return}
set restCodesText to restCodes as text
set text item delimiters to TID
else
set restCodesText to ""
end if
set eof fileRef to 0
write restCodesText to fileRef
close access fileRef
return currentCode
on error errorMessage number errorNumber
if errorNumber = -39 then
display dialog "No codes available" buttons {"OK"} default button 1
else
display dialog errorMessage buttons {"OK"} default button 1
end if
try
close access file codesFile
end try
end try
return missing value
end run