Hi. This is my first time posting. I’d like to be able to set a text string to the title of a file.
What I’m trying to do is to use Abbyy Finereader Express to OCR jpegs. I would like this to happen in the background. I have most of the script working. But I need to be able to not have to decide the filename of the newly created .txt file. I’d like it to be the exact same as the .jpg file.
I’d like to be able to do this without using the copy and paste commands if possible.
Model: iMac
AppleScript: 2.6.1
Browser: Safari 537.85.10
Operating System: Mac OS X (10.8)
If you the file’s displayed name doesn’t show extensions, then you can just get ‘displayed name’ with System Events. If you’re not sure that the extension is displayed or not, then something like this:
-- first get a reference to the file
set file_ref to choose file
-- get full name of the file
tell application "System Events" to set file_name to name of file_ref
--get file name without extension
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set temp_list to text items 1 through -2 of file_name
set file_name to temp_list as text
set AppleScript's text item delimiters to tids
return file_name
Edited: don’t know if you know how to create the text file name. To do that concatenate the extension:
-- first get a reference to the file
set file_ref to choose file
-- get full name of the file
tell application "System Events" to set file_name to name of file_ref
--get file name without extension
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set temp_list to text items 1 through -2 of file_name
set end of temp_list to "txt"
set new_name to temp_list as text
set AppleScript's text item delimiters to tids
return new_name
Thanks Kel.
That seems to work on its own. How do I integrate it into my current script?
tell application “Finder”
set filePathAlias to (choose file)
set fileName to name of filePathAlias
set filePath to filePathAlias as string
end tell
tell application “ABBYY FineReader Express”
activate
end tell
delay 2
tell application “System Events”
tell process “ABBYY FineReader Express”
click button “Convert” of window “ABBYY FineReader Express”
set windex to count every window
repeat until windex = 2
set windex to count every window
say “scanning”
delay 3
end repeat
delay 2
say “completed”
click button “Save” of front window
end tell
end tell
Model: iMac
AppleScript: 2.6.1
Browser: Safari 537.85.10
Operating System: Mac OS X (10.8)
I’m not sure how your save dialog looks, but I think the script goes in your script something like this:
tell application "Finder"
set filePathAlias to (choose file)
set fileName to name of filePathAlias
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set tempList to text items 1 through -2 of fileName
set end of tempList to "txt"
set newName to tempList as text
set AppleScript's text item delimiters to tids
-- from here use newName for your text file. I'm guessing that you could paste it into the save window text field or maybe you can use the Save command? I don't know.
-- to paste, use 'set the clipboard to newName'
-- then with System Events ui scripting 'keystroke "v" using command down'
-- focus needs to be in the text field
tell application "Finder"
set filePath to filePathAlias as string
end tell
tell application "ABBYY FineReader Express"
activate
end tell
delay 2
tell application "System Events"
tell process "ABBYY FineReader Express"
click button "Convert" of window "ABBYY FineReader Express"
set windex to count every window
repeat until windex = 2
set windex to count every window
say "scanning"
delay 3
end repeat
delay 2
say "completed"
click button "Save" of front window
end tell
end tell
Using the paste way, it should look something like this:
tell application "Finder"
set filePathAlias to (choose file)
set fileName to name of filePathAlias
end tell
-- added this
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set tempList to text items 1 through -2 of fileName
set end of tempList to "txt"
set newName to tempList as text
set AppleScript's text item delimiters to tids
set the clipboard to newName
-- end
tell application "Finder"
set filePath to filePathAlias as string
end tell
tell application "ABBYY FineReader Express"
activate
end tell
delay 2
tell application "System Events"
tell process "ABBYY FineReader Express"
click button "Convert" of window "ABBYY FineReader Express"
set windex to count every window
repeat until windex = 2
set windex to count every window
say "scanning"
delay 3
end repeat
delay 2
say "completed"
click button "Save" of front window
end tell
keystroke "v" using command down -- added this
end tell
Thanks. Your script is returning this error message.
error “System Events got an error: Can’t get button "Save" of window 1 of process "ABBYY FineReader Express".” number -1728 from button “Save” of window 1 of process “ABBYY FineReader Express”
I’m still having to press the save button. Is there a way I can avoid having to do that?
Model: iMac
AppleScript: 2.6.1
Browser: Safari 537.85.10
Operating System: Mac OS X (10.8)
set windex to count every window
repeat until windex = 2
set windex to count every window
and
delay 3
end repeat
delay 2
The script worked better. It doesn’t get stuck now. It looks like this now:
tell application "Finder"
set filePathAlias to (choose file)
set fileName to name of filePathAlias
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set tempList to text items 1 through -2 of fileName
set end of tempList to "txt"
set newName to tempList as text
set AppleScript's text item delimiters to tids
-- from here use newName for your text file. I'm guessing that you could paste it into the save window text field or maybe you can use the Save command? I don't know.
-- to paste, use 'set the clipboard to newName'
-- then with System Events ui scripting 'keystroke "v" using command down'
-- focus needs to be in the text field
tell application "Finder"
set filePath to filePathAlias as string
end tell
tell application "ABBYY FineReader Express"
activate
end tell
delay 2
tell application "System Events"
tell process "ABBYY FineReader Express"
click button "Convert" of window "ABBYY FineReader Express"
say "scanning"
say "completed"
click button "Save" of front window
end tell
end tell
How can I get it to automatically choose any file that is put into the folder. At the moment I have to choose the file.
You know what doesn’t look right in the script is that the program seems like it already has a file opened in it. There is no where that the reader is opening the file that is chosen. That’s why I thought that the ‘choose file’ was there to get a reference to the file.
There are several ways to monitor a folder so that when you add something to it something happens. What you might want to try is a Folder Action. I think there’s a tutorial on this site about folder actions. Try searching, or maybe somebody might know where it is.
In AppleScript Editor Preferences, check Show Script menu in menu bar. You’ll see the Script menu in the right side of the menu bar. When you click on the script icon in the menu bar, you’ll see Folder Actions. The main things you want right now is “Attach Script to Folder” and “Enable Folder Actions”. You can enable it or disable it at anytime.
First you need to write your folder action script. In Script Editor, open the StandardAdditions dictionary. You’ll find a section on Folder Actions. What you want is the handler:
Basically, your script will look like this:
on adding folder items to thisFolder after receiving theseItems
-- your script goes here
end adding folder items to
thisFolder will be a reference to the folder that you have attached the folder action script to and theseItems will be a list of references to the items that were added to the folder.
3. Save the script in your Home > Library > Scripts > Folder Actions script folder.
4. After you have written your script, you need to attach it. Go to the Script menu in the menu bar and under Folder Actions, Attach Script to Folder. In the dialog, choose what script to attach and the folder to attach it to.
Man I wish there was a tutorial somewhere. Never new it was this complicated. You might try experimenting on a simple folder action script and a test folder.
You know what you should do is like make a plan of what you’re trying to do. You’re trying to reach some objective. Then just go step by step instead of trying to do the whole thing at one time. Ask questions for each step. The thing is that a person needs to be organized in what they are trying to do. If not organized then you’ll be all over the place.
Here is my edited script:
I followed your advice and attached it to the folder. But when I added a new jpeg nothing happened. Is there a free script debugger you can recommend?
Thanks for your help.
on adding folder items to thisFolder after receiving theseItems
tell application "Finder"
set filePathAlias to theseItems
set fileName to name of filePathAlias
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set tempList to text items 1 through -2 of fileName
set end of tempList to "txt"
set newName to tempList as text
set AppleScript's text item delimiters to tids
-- from here use newName for your text file. I'm guessing that you could paste it into the save window text field or maybe you can use the Save command? I don't know.
-- to paste, use 'set the clipboard to newName'
-- then with System Events ui scripting 'keystroke "v" using command down'
-- focus needs to be in the text field
tell application "Finder"
set filePath to filePathAlias as string
end tell
tell application "ABBYY FineReader Express"
activate
end tell
delay 2
tell application "System Events"
tell process "ABBYY FineReader Express"
click button "Convert" of window "ABBYY FineReader Express"
say "scanning"
say "completed"
click button "Save" of front window
end tell
end tell
end adding folder items to
Hi Kel,
Thanks for your advice.
You’re right. I should have written down the list of what I wanted to achieve with my script.
I nearly have everything achieved now. I just need the script to run automatically when new items are added to the folder.
I would also like to move the newly created .txt files to a new folder.
After that I’m going to use filemaker pro to import the contents of both folders to a database.
When that is finished I’d like the contents of the .jpg folder and the .txt to be deleted. This will save space and also let. I’ll know if there are contents in this folder that I need to run the filemaker pro import script.
The variable theseItems is a list of references to the items that were dropped. If you know that there is only one item, then you can just get item 1. Note that there will always be at least one item except on rare occasions. If you drop more than one item, then people usually use a repeat loop to process every item. I’ll write a quick example.
Here’s a quick folder script that just displays the full name of each dropped item:
on adding folder items to thisFolder after receiving theseItems
-- theseItems is a list of one or more items
repeat with thisItem in theseItems
-- thisItem is an alias reference to a dropped item
tell application "Finder"
set itemName to name of thisItem
end tell
display dialog itemName
end repeat
end adding folder items to
With folder actions you need to wait at least 10 seconds (by minimum default) from the time the items are dropped until the time they are completely processed or wierd things might happen.
Don’t give up, you’ll get there. Also there are many good examples throughout this site.
Edit: here’s the same example except that might want to just get the first item. This is better if you know there is only one item, otherwise you’re placing your whole script in another repeat loop which sometimes cause problems:
on adding folder items to thisFolder after receiving theseItems
-- theseItems is a list of one or more items
set thisItem to item 1 of theseItems
-- thisItem is an alias reference to a dropped item
tell application "Finder"
set itemName to name of thisItem
end tell
display dialog itemName
end adding folder items to
Edited: one more thing. Don’t rename a file while it is still in the folder action folder, otherwise, the folder action will think that it is a new item. That’s not what you’re doing, but just in case you might do that someday.
Hi Kel. I’ve updated the script.
Its running quite well. But the new file which is being saved in the folder is causing the script to trigger again.
I think I need to set a target folder for where I need to save the new file.
Or could I move the new file as part of the script like what I have done in this script? I don’t think so as its not working.
Is there a way of debugging scripts?
Forgive the garbled talk. Its quite late at night and my concentration is waning.
Thanks for your advice so far. Its been really helpful.
on adding folder items to thisFolder after receiving theseItems
-- theseItems is a list of one or more items
set thisItem to item 1 of theseItems
-- thisItem is an alias reference to a dropped item
tell application "Finder"
set fileName to name of thisItem
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set tempList to text items 1 through -2 of fileName
set end of tempList to "txt"
set newName to tempList as text
set AppleScript's text item delimiters to tids
-- from here use newName for your text file. I'm guessing that you could paste it into the save window text field or maybe you can use the Save command? I don't know.
-- to paste, use 'set the clipboard to newName'
-- then with System Events ui scripting 'keystroke "v" using command down'
-- focus needs to be in the text field
tell application "Finder"
set filePath to thisItem as string
end tell
tell application "ABBYY FineReader Express"
activate
end tell
delay 2
tell application "System Events"
tell process "ABBYY FineReader Express"
click button "Convert" of window "ABBYY FineReader Express"
say "scanning"
say "completed"
click button "Save" of front window
end tell
end tell
tell application "Finder"
move every item of "Hard disc:Users:peter:Desktop:display" to desktop
end tell
end adding folder items to
The Script Editor can’t simulate running folder action scripts. They need to be attached to folders. When you ran it in the AppleScript Editor it’s just a handler and nothing will happen you run it.
Maybe someone might have the application “ABBYY FineReader Express”. After you get this folder action thing working and the parsing of the name with new extension, you might want to write a new post. Then someone with that application might now better what is a good way to do what you are doing.
It’s running now Kel when I put it a JPEG in the folder.
The new file is saving in the same folder causing an infinite loop like you mentioned earlier.
How do I choose a different folder to save the .txt file in?