set text string to file name

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

Hi Peter,

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.

Good Luck,
kel

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?

Hi Peter,

Is the application automatically saving to the same folder? If it is, then you might need to use a droplet instead.

Glad you have it working.

Edited: wait let me think about this>

Later,
kel

In your script, I don’t see how “ABBYY FineReader Express” knows where the file is located. it looks like you need to change the file path, but I don’t understand what is happening. Is that the whole script that you last posted?

I think that you manually set the folder where the reader should save the file (just guessing). You might use a toggler.

When the jpeg is dropped, you processing script runs. It then switches a variable, so that when the text file is added by Abby reader a different script runs. I’ll make an example.

Later,
kel

Hi Peter,

Something like this toggler:

property doProcess : true

if doProcess is true then
	say "processing j peg"
else
	say "moving text file"
end if
set doProcess to not doProcess

Try running this in AppleScript Editor.

Later,
kel

Another way. Instead of the toggled, you can check the extension:

set f to choose file
tell application "System Events"
	set theExtension to name extension of f
end tell
if theExtension is "jpg" then
	say "processing j peg"
	return
else
	say "moving text file"
end if

Wait. Instead of the above, try this:

set f to choose file
tell application "System Events"
	set theExtension to name extension of f
end tell
if theExtension is "jpg" then
	say "processing j peg"
	return
end if
say "moving text file"

I’m doing two things at the same time. :slight_smile:

Hi Kel,
Once I manually choose a folder in Abbyy Finereader it automatically makes that folder the default folder so choosing a location for .txt files is not a problem anymore.
I now have two scripts attached to my folder. One that does OCR on my jpeg and another that moves the processed jpeg to another folder.

  1. I only know how to move the jpeg to the desktop. I’ve tried to write the file path like this “hard disc/Users/peter/Desktop” But it doesn’t seem to accept it. Can you tell me the correct syntax please? I’ve searched a bit and haven’t been able to find it.
  2. Also I don’t want to paste new name into the “Save As:” field. Is there a way I can insert it into the field so that it always saves as the same name as the jpeg?
    Here is the move txt script:
on adding folder items to thisFolder after receiving theseItems
	set thisItem to item 1 of theseItems
	tell application "System Events"
		set theExtension to name extension of thisItem
	end tell
	if theExtension is "jpg" then
		tell application "Finder"
			move thisItem to "hard disc/Users/peter/Desktop"
		end tell
		return
	end if
	say "moving text file"
end adding folder items to

Thanks for your help. I think we’re nearly there.

Hi Peter,

This is posix style reference:

An easy way to see a reference that Finder can use is to run ‘choose file’ or 'choose folder in the AppleScript Editor and look at the result:

choose file

What you get is an alias reference.

Here’s a good article on references:
http://macscripter.net/viewtopic.php?id=24575
Basically, there are three types of references that Finder can use.

Thinking about how you can enter the file name, but your application is not scriptable I think. So there probably isn’t another way to enter the name in the save dialog. I wish I had your app.

Edited: actually, there might be a way to avoid pasting the name in the save dialog, but it still uses ui element scripting. You probably can set the value of the text field in the save dialog. If it works, then you wouldn’t need to copy the name to the clipboard. I’ll try it on a TextEdit save dialog.

gl,
kel

Hi Peter,

It works in TextEdit. I used this with the save sheet opened:

tell application "TextEdit"
	activate
end tell
tell application "System Events"
	tell process "TextEdit"
		tell window "Untitled"
			tell sheet 1
				tell text field "Save As:"
					set value of attribute "AXValue" to "hello"
				end tell
			end tell
		end tell
	end tell
end tell

I don’t know how to make it work in your application though. Maybe you can post a question about this if you don’t know how to do it. Also, this probably won’t work on non-English systems. The wording is different and I don’t know how to get the indices for the ui elements.

Edited: and btw, this has been nagging at my brain. :slight_smile: Why would you want to convert a jpeg to text file? To look at the header? Because, you can just read the file to get the header I think.

gl,
kel

Hi. I’m scanning lots of blurbs from old books. I want to import them as text to my database.
I want to import folders of text files and folders of jpegs. Then I can check the blurb text against the photograph to check and see if the content of the blurb scanned accurately.
What does

mean?
Here is the updated applescript:


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"
			tell text field "Save As:"
				set value of attribute "AXValue" to "hello"
			end tell
			click button "Save" of front window
		end tell
	end tell
end adding folder items to

Hi Peter,

AXValue is an attribute of a ui element (object). In the System Events dictionary they’re listed as properties of ui element. If you use Xcode’s Developer Tools, Accessibility Inspector, they are listed as attributes. Accessibility Inspector makes it easier to see the ui elements in the path to the ui element that you’re trying to script. It’s easy to use. And it also list some actions that you might want to use.

I guess you got the script working then. Good job.

Edited: come to think of it, you might be able to just write ‘set value of someUIElement to someValue’. Not sure though, but you can experiment with that.

Good Luck,
kel

Thanks for all your help Kel.
I may have more questions for you soon.:stuck_out_tongue:

Ask away man. There are gurus around here.

And, btw, It’s good that you moved the items to the other folder with the other folder action. Remember that it’s not good to drop files to the folder within a threshold of 10 seconds. When your application converted the files and placed it back into the same folder, it might have done it in less than 10 seconds and weird things might have occurred.

Hi Kel.
I’ve tried to simplify this script to find out why it is not triggering with folder actions anymore.
I wrote a simple folder actions script and attached it to a folder and it is not working.
Since I launched Yosemite the script which you helped me write is not functioning.
Here is my simple script.


on adding folder items to thisFolder after receiving theseItems
	say "hello"
end adding folder items to

Is there something different I need to do with Yosemite?
Here is the script which I want to work:


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
end adding folder items to

I found the problem. I had a list of scripts in my folder actions which were misfiring. I deleted all of the unwanted folder actions and its working fine for the moment.

This script seems repeats the scan on the same file even when I have a new file in the folder. Is there a way to get it to scan the new file that goes into the folder and not the old file thats not even there any more?
This is how I have the script now.


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"
-- I need a command in here that will choose the thisitem. 
			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

Is there a way to get thisitem to open in application “FineReader Express”?