Would you mind doing me a favour and just sending me the PDFs that you are using for testing purposes ? That way, I can see what sorts of content you're dealing with.
This latest error, whilst very similar in nature to the one affecting the Title of the PDF document, seems odd that it would arise when dealing with a page from the PDF. It implies that there was no language detected, which in turn implies the page had no text. I can perhaps believe this might be the case for one random PDF file, but if you're presumably testing different files, then the others ought to work.
Well, that took much more than just a minute. Partly because your workflow was just kinda ugh, and partly because Objective-C is really bloody annoying on occasion, and the AppleScript needed to be re-written to cope with multiple file inputs, and because Automator can't do repeat loops by itself.
Here's a screenshot of the Automator workflow, which now only has four actions:
The modified AppleScript for use in the Run AppleScript action is below. Largely, the screenshot and script are for the benefit of anyone viewing this post at a later date, from which they can piece together the workflow themselves because I still haven't set up a base for storing permanent links to fileshares, so this one will be temporary:
Append Language To Name of PDF File.workflow.zip
use framework "Foundation"
property this : a reference to current application
property NSFileManager : a reference to NSFileManager of this
property NSLinguisticTagger : a reference to NSLinguisticTagger of this
property NSString : a reference to NSString of this
property nil : a reference to missing value
on run [fs, null]
script fileURLs
property list : fs
end script
set FileManager to NSFileManager's defaultManager()
repeat with f in the list of fileURLs
try
set lang to "_" & ((NSLinguisticTagger's ¬
dominantLanguageForString:(NSString's ¬
stringWithContentsOfURL:f)) as text)
set basename to (NSString's stringWithString:(f's ¬
POSIX path))'s stringByDeletingPathExtension()
set oldname to (basename's ¬
stringByAppendingPathExtension:"pdf")
set newname to ((basename's ¬
stringByAppendingString:lang)'s ¬
stringByAppendingPathExtension:"pdf")
tell the FileManager to moveItemAtPath:oldname ¬
toPath:newname |error|:nil -- Rename PDF file
tell the FileManager to trashItemAtURL:f ¬
resultingItemURL:nil |error|:nil -- Delete text file
end try
end repeat
end run
Okay then. The best way to do this is probably to simulate the CMD+OPT+Right Arrow keypress with AppleScript, like this:
tell application "System Events" to key code 124 using {command down, option down}
In an Alfred workflow, put that in a Run Script Action with Language = /usr/bin/osascript (AS).
It's up to you to ensure that Finder is the active application and the correct folder is selected, however. If you want to automate that, too, this will show your home folder in Finder in List View:
property theTarget : get path to home folder tell application "Finder" activate tell front Finder window set current view to list view reveal theTarget end tell end tell
Can't say much more than that without more details of precisely what you want to do (open the same folder every time, choose the folder to open in Alfred etc.)