Shane’s article ADDING APPLESCRIPTOBJC TO EXISTING SCRIPTS employs the conceit that you may want to add ASObjC code to an existing non-ASObjC script containing loads of file specifiers and you don’t have time to go through changing them all to «class furl» coercions.
If you do have more time, you may be able save some of it by using the following regex in Script Debugger’s “Find” field (with the “RegEx” box checked, of course):
This matches the word ‘file’ followed by anything which could be a variable label, text in double-quotes, or a parenthesised expression up to three levels deep.
The corresponding replacement string would be:
($1 as «class furl»)
In use, it’s better to step through finds using “Replace & Find” than to hit “Replace All”, since you could easily end up doing peculiar things to comments or application references:
If you put that pattern in regex101.com, it will give you a nice explanation, although it may not be as good as Nigel’s. If you also provide some sample text, it will show you the matches.
set filePath to (path to desktop as text) & "Test.txt"
set _file_path to (path to desktop as text) & "Test.txt"
-- This has to be skipped manually.
set |file path with quotes| to (path to desktop as text) & "\"Test\".txt"
-- The file specifiers here are all correctly matched.
set theText to read file filePath
set theText to (read file filePath)
set theText to (read file _file_path)
set theText to (read file |file path with quotes|)
set theText to (read file "My Disk:Users:me:Desktop:Test.txt")
set theText to (read file "My Disk:Users:me:Desktop:\"Test\".txt")
set theText to (read file ((path to desktop as text) & "Test.txt"))
set theText to (read file ((path to desktop as text) & "\"Test\".txt"))
set theText to (read file ((path to home folder as text) & "Desktop:" & "\"Test\".txt"))
set theText to (read file ((path to home folder as text) & ("Desktop:" & "\"Test\".txt")))
set theText to (read file (((path to home folder as text) & "Desktop:") & ("\"Test\"" & (|the dot| & "txt"))) as «class utf8»)
-- These have to be skipped manually.
-- The file is now ready.
tell application "Finder" to open file "Test.txt" of desktop