I’ve been searching for the answer but not having much luck, I need to check that a filename only contains numbers. If there are any other characters then I need it to error, can anyone point me in the right direction?
Hi,
just coerce the string to integer. If the string contains non-numeric characters, an error is thrown
set filename to "1235"
set isNumeric to checkStringIsNumeric(filename)
on checkStringIsNumeric(theString)
try
theString as integer
return true
on error
return false
end try
end checkStringIsNumeric
Thanks for the speedy response, I’ve tried to include that into an existing script but it’s not playing nicely. The script I am using checks sections of the filename for information and will save it to a different location based on that information.
We recently changed how the filenames are saved and now the section that decides which week folder to save the file has to be numerical only, this is referred to as theWeek in the script. I’ll include it below so you can see what I mean.
At the moment I just have it checking theWeek for specific codes we used to use, but I’d like to be safe and block any text at all in that section. You can find it under the comment
(Check for DT, TG in week to make sure it’s not an old code artwork)
(*
WorkFlow Version 0.7.004
-- 0.1 Initial Build
-- 0.3 Added file monitor routine to bypass slow network transfers
-- 0.5 Watched Folder version via Lingon - runs ever x minutes as set in the launchd job
-- 0.7 Added support to trap for following errors:
¢ invalid source files
¢ improperly named files
¢ failed pdf generation
.004 - fixed log file handling
-- 0.8 Added File and Creator change to PDF and CARO
*)
global theWatchedFolder, ADresourceFolder, lowOptions, highOptions, lowOutputFol, highOutputFol, chainList, lowServerList, highServerList, printerList, failedFolder
on run
-- Declare Paths to Resources
set theWatchedFolder to "Server_HD:PDF_Hotfolder:PDF_HotFolder_2"
set ADresourceFolder to "Server_HD:PDF_Hotfolder:Distiller Resources:"
set lowOptions to ADresourceFolder & "joboptions:VC_CoolTick.joboptions"
set highOptions to ADresourceFolder & "joboptions:High_Res.joboptions"
set lowOutputFol to ADresourceFolder & "lowHolding:"
set highOutputFol to ADresourceFolder & "highHolding:"
set failedFolder to "Server_HD:PDF_Hotfolder:FailedJobs_2"
set chainList to {"1", "2", "3", "5", "6", "7", "9", "D", "P", "R", "H", "I"}
set lowServerList to {":Volumes:POS_LR:POS_LR:"}
set highServerList to {":Volumes:POS_HR:POS_HR:"}
set printerList to {"Toshiba_102"}
-- Mount volumes if required
tell application "Finder"
if not (exists "POS_LR") then
try
mount volume "afp://pdfspare:pdfspare@164.38.219.44/POS_LR"
end try
end if
if not (exists "POS_HR") then
try
mount volume "afp://pdfspare:pdfspare@164.38.219.44/POS_HR"
end try
end if
end tell
--Begin workflow
tell application "Finder"
set theFiles to every file of folder theWatchedFolder
repeat with i from 1 to (count theFiles)
set aFile to item i of theFiles
if (my _verifyFile(aFile, true)) then
my _lowChain(aFile)
my _highChain(aFile)
end if
move aFile to trash
--empty the trash--
end repeat
end tell
end run
-- Distiller handler to generate low/high res PDFs
to _Distill(aFile, outputPath, jobOptions)
set aFilePosix to POSIX path of (aFile as Unicode text)
tell application "Acrobat Distiller"
set theResult to Distill sourcePath aFilePosix adobePDFSettingsPath (POSIX path of jobOptions) destinationPath (POSIX path of outputPath)
end tell
return theResult
end _Distill
-- Rename the PDF and store in proper location
to _storePrintFile(suffix)
tell application "Finder"
if suffix is "_LR" then
set aPDF to item 1 of (get files of folder lowOutputFol) as alias
else
set aPDF to item 1 of (get files of folder highOutputFol) as alias
end if
my _verifyFile(aPDF, false)
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "_"}
set {name:Nm, name extension:Ex} to (info for aPDF)
if Ex is missing value then set Ex to ""
if Ex is not "" then
set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set Ex to "." & Ex
end if
set fileName to Nm & suffix & Ex
tell application "Finder"
set file type of aPDF to "PDF "
set creator type of aPDF to "CARO"
end tell
set name of aPDF to fileName
try
set {theGroup, na, theWeek} to text items of fileName
set chain to text 1 thru 1 of theGroup
set theFolder to text 1 thru 1 of theGroup
on error
-- an error has occured with the file name, trick the script into a .log extension to trigger error handling
set Ex to ".log"
end try
set AppleScript's text item delimiters to tid
if Ex is equal to ".log" then
move aPDF to folder failedFolder with replacing
return false
end if
(*Check for DT, TG in week to make sure it's not an old code artwork*)
try
if theWeek contains "DT" then
move aPDF to folder failedFolder with replacing
return false
else if theWeek contains "TG" then
move aPDF to folder failedFolder with replacing
return false
end if
end try
if chain is in chainList then
repeat with i from 1 to (count chainList)
if chain is equal to (item i of chainList) then
if suffix is "_LR" then
delay 1
set theServer to POSIX path of (item 1 of lowServerList)
exit repeat
else
set theServer to POSIX path of (item 1 of highServerList)
(*
set theServer to POSIX path of lowServerList
exit repeat
else
set theServer to POSIX path of highServerList
*)
tell application "Printer Setup Utility" to set current printer to printer (item 1 of printerList)
tell application "Adobe Acrobat Pro"
open aPDF
print pages active doc PS Level 3 with binary output and shrink to fit
close active doc
end tell
exit repeat
end if
end if
end repeat
try
set command to "/bin/mv -f " & (quoted form of (POSIX path of aPDF)) & space & (quoted form of theServer) & "Week" & (text 1 thru 2 of theWeek) & "/" & theFolder & "*/"
do shell script command
return true
on error
move aPDF to folder failedFolder with replacing
return false
end try
else
move aPDF to folder failedFolder with replacing
return false
end if
end tell
end _storePrintFile
-- Low Res Chain handler
to _lowChain(aFile)
set lowResult to my _Distill(aFile, lowOutputFol, lowOptions)
if lowResult is not 1 then
--handle a error here on generation
return false
end if
-- also need to verify not a .log file
my _storePrintFile("_LR")
end _lowChain
-- High Res Chain handler
to _highChain(aFile)
set highResult to my _Distill(aFile, highOutputFol, highOptions)
if highResult is not 1 then
--handle a error here on generation
return false
end if
-- also need to verify not a .log file
my _storePrintFile("_HR")
end _highChain
to _verifyFile(aFile, fullVerify)
repeat
set was to (info for (aFile as alias) with size)
delay 8
set isNow to (info for (aFile as alias) with size)
if was = isNow then exit repeat
end repeat
if fullVerify then
tell application "Finder"
if name extension of (info for (aFile as alias)) is not "eps" then
move aFile to folder failedFolder with replacing
return false
end if
end tell
end if
return true
end _verifyFile
Oh and excuse the mess of the script, it’s been tweaked and chopped many times since I received it!
Hello.
Are you going to check that the filename only contains digits in the verify file handler during fullverify?
- Or do you intend to implement another handler to run after fullverify?
Hi,
I’d like it to run as early as possible, so if the filename does contain anything other than numbers it can just error and fail.
Hello.
I have made a handler for you to incorporate in your script, and changed the block below the comment " --Begin workflow". The code may not be optimal, but is should work, and be readable.
on hasDigitFilename(aFile)
set aFile to aFile as alias
set aFileText to aFile as text
tell (a reference to AppleScript's text item delimiters)
set {oldtids, contents of it} to {contents of it, ":"}
set fn to item -1 of (text items of aFileText)
set contents of it to "."
set fn to item 1 of (text items of fn)
set failed to false
try
set nm to fn as integer
on error
set failed to true
end try
set contents of it to oldtids
end tell
if failed then
return false
else
return true
end if
end hasDigitFilename
Here is the changed block.
repeat with i from 1 to (count theFiles)
set aFile to item i of theFiles
if (my hasDigitFilename(aFile)) then
if (my _verifyFile(aFile, true)) then
my _lowChain(aFile)
my _highChain(aFile)
end if
move aFile to trash
--empty the trash--
end if
end repeat