I am writing a script that checks its own name to decide which actions to perform. If the name contains “RTF”, it generates an .RTF file; if the name contains “DOC”, it generates a .DOC file; if the name contains “PDF”, it generates a .PDF file. If it contains none of those strings, it performs its default action.
My problem is this: is there an elegant way to write code that will tell the user not to use two or three of those strings in the name of the script? Right now, I’ve got to the point where I have this sloppy code (which sets various variables that my script need), based partly on other posts in this forum:
set myPath to path to me as text
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
-- outString defaults to doc, but we can change it here to others
if myName contains "pdf" then
set outString to {"pdf" as Unicode text}
set doPDF to true
else if myName contains "doc" then
set outString to {"doc" as Unicode text}
set doPDF to false
else if myName contains "rtf" then
set outString to {"rtf" as Unicode text}
set doPDF to false
end if
But I want to warn the user if he uses two or three of the strings “RTF”, “Doc”, “PDF”, and I don’t want to script all the possibilities.
I think I see that I need to put the three strings into an array and then check if the variable myName contains more than 1, and then build a dialog message that contains the two or three strings, but I haven’t been able to figure out how to do this. If anyone is willing to help, I would be very grateful. Thank you!
set myPath to path to me
set {name:fileName, name extension:fileExtension} to info for myPath
if fileExtension is missing value then set fileExtension to ""
if fileExtension is not "" then set fileName to text 1 thru ((count fileName) - (count fileExtension) - 1) of fileName
if checkType(fileName, "pdf") then
set outString to {"pdf"}
set doPDF to true
else if checkType(fileName, "doc") then
set outString to {"doc"}
set doPDF to false
else if checkType(fileName, "rtf") then
set outString to {"rtf"}
set doPDF to false
end if
on checkType(aName, aType)
set {TID, text item delimiters} to {text item delimiters, aType}
set occurrences to count text items of aName
set text item delimiters to TID
if occurrences < 2 then
return false
else if occurrences = 2 then
return true
else
display dialog "too many occurrences of type " & aType
return false
end if
end checkType
My reading of the question is that you only want one of those possibilities in the name rather than just one instance of the first hit. This could probably be tidied up, but it does what I think you want:
try
set myName to name of (info for (path to me)) -- 'info for' is deprecated, but still works at the moment.
on error
tell application "System Events" to set myName to name of (get properties of (path to me))
end try
try
set doPDF to (myName contains "pdf")
if (doPDF) then set outString to {"pdf"}
set doDoc to (myName contains "doc")
if (doDoc) then
if (doPDF) then error
set outString to {"doc"}
end if
set doRTF to (myName contains "rtf")
if (doRTF) then
if ((doPDF) or (doDoc)) then error
set outString to {"rtf"}
end if
set defaultAction to not ((doPDF) or (doDoc) or (doRTF))
on error
display dialog "My name must contain one (only) of these sequences:
"pdf", "doc", "rtf"." buttons {"OK"} with icon stop
error number -128
end try
if (defaultAction) then
"Default action"
else
{doPDF, outString}
end if
Stefan, your code taught me a technique I didn’t know before, so I am very glad to have it. But I should have been more clear about what I was asking for, which was this: I don’t want the user to put both PDF and RTF, or both PDF and DOC, or both RTF and DOC, or all three, in the filename.
Nigel, your code looks elegant and efficient, and does exactly what I was hoping for. I’m grateful for the code and impressed with the logical and elegant thinking that went into it.