wait while file is written

Hi
I want to wait for file to be processed by ABBYY FineReader for ScanSnap before it quits, its a issue for large file

Here is my attempt


tell application "Finder"
	set theFile to selection
	set fileSizeO to size of (info for theFile)
end tell
tell application "ABBYY FineReader for ScanSnap"
	recognize theFile and export to theFile as pdf with silent mode
end tell
-- I thought the size wud change after processing, so reCheck the size ? is it appropriate
tell application "Finder" to set fileSize to size of (info for theFile)
repeat until fileSize ≥ fileSizeO
	delay 1
end repeat

tell application "ABBYY FineReader for ScanSnap" to quit


Off course this does not work

Cud this Busy be used to determine if file is processed

Cheers

Probably it is difficult to find an experienced scriptwriter who has this application and already scripted it. Therefore, I will try to help by working blindly. I cannot help with the busy property due to the lack of a dictionary. But I will try to fix the parts of the script that are familiar.

So, until you get a better solution, test this:


-- Get source file size, make new empty destination file (to avoid security problems)
tell application "Finder"
	set sourceFile to item 1 of (get selection) as alias
	set theName to name of sourceFile
	set fileSizeO to size of (info for sourceFile)
	set destinationFile to (make new file at desktop with properties {name:theName}) as alias
end tell

-- open source file, scan document and export it
tell application "ABBYY FineReader for ScanSnap"
	set aDocument to open sourceFile
   recognize aDocument and export to file (destinationFile as text) as pdf with silent mode
end tell

-- wait until exporting teminates
repeat until (size of (info for destinationFile)) ≥ fileSizeO
   delay 1
end repeat

-- close all
tell application "ABBYY FineReader for ScanSnap"
 close documents saving no
 quit
end tell

There is a post which includes a script provided by Abbyy’s support that uses ‘busy’ to determine whether the script should go into a holding pattern. You can read the whole thing here but below are the specific handlers used in it:

https://macscripter.net/viewtopic.php?pid=178759#p178759

on WaitWhileBusy()
   repeat while IsMainApplicationBusy()
   end repeat
end WaitWhileBusy

on IsMainApplicationBusy()
   tell application "FineReader OCR Pro"
       set resultBoolean to is busy
   end tell
   return resultBoolean
end IsMainApplicationBusy

In essence, the first handler is called when you want to check busy. It then calls the second handler which returns true until it returns false, which lets the first handler finish and the script proceed.

Note that the name is different than the OP’s. I use ‘FineReader’ myself. I don’t know how similar the scripting functionality would be between the three versions. On my system however, ‘busy’ will return true while the app is inhaling a large document and then false when it’s complete, so if the command is available in your version, it should work.

FWIW, I don’t think that the size trick would work reliably. If the file increased in size incrementally, then even a 1 byte change would allow the script to proceed.

Does your version’s dictionary include ‘get document progress’?

Here is something to try. As mentioned above, my version is not the same as yours. Mine uses different syntax for ‘export’. I include what might work for you in comments.

If you get an error around is finereader controller active then your version presumably doesn’t support that. Strip the if…then block and consider placing WaitWhileBusy() there instead (if you need a delay before the export begins). It might be better to move WaitWhileBusy() to immediately above the quit command.

tell application "Finder"
	set tfa to selection as alias
	set tf to POSIX path of (selection as alias)
	set tn to name of tfa
	set op to (path to desktop) & "op.pdf" as text
end tell

tell application "FineReader"
	activate
	if (is finereader controller active) is true then
		
		export to pdf op from file tf
		-- export to op as pdf with silent mode
		
		tell me to WaitWhileBusy()
		
	end if	
	quit
end tell

on WaitWhileBusy()
	repeat while IsMainApplicationBusy()
	end repeat
end WaitWhileBusy

on IsMainApplicationBusy()
	tell application "FineReader"
		set resultBoolean to is busy
	end tell
	return resultBoolean
end IsMainApplicationBusy

Hi, Dictionary does not include ‘get document progress’
Really does not have a lot in Dictionary (at least to me (newbie))

ABBYY FineReader for ScanSnap Suite ABBYY FineReader for ScanSnap specific classes.
export format enum
pdf : Searchable PDF Document.
word document : Microsoft Word Document.
excel spreadsheet : Microsoft Excel Workbook.
power point presentation : Microsoft Power Point Presentation.
show preferences v : Shows ABBYY FineReader for ScanSnap preferences.
show preferences
recognize v : Recongizes a document and exports it to the specified format.
recognize file
[and export to file] : Specified destination file.
[as pdf/word document/more…] : Export format.
[deprecated remove input files boolean] : Deprecated remove import files.
[silent mode boolean] : Run application in silent mode.
application n [inh. application; see also Standard Suite] : The application’s top-level scripting object.
properties
busy (boolean, r/o) : Is ABBYY FineReader for ScanSnap application busy?
responds to
show preferences, recognize.

Hi Mockman

tried the waiting Handler →throws an error

Hi KniazidisR
This seems to work :slight_smile:

tell application "Finder" 
set theFile to selection as alias
set fileSizeO to size of (info for theFile)
end tell
tell application "ABBYY FineReader for ScanSnap"
	recognize theFile and export to theFile as pdf with silent mode
repeat until (size of (info for theFile)) is not equal to fileSizeO
	delay 1
end repeat
quit
end tell

I think it’s partly related to your version. Did it come along with a scansnap scanner? If so, it looks like it has more limitations. I’m using FineReader Pro for Mac v12 and it has a decent dictionary (20+ commands).

What comes to mind is does your script always refer to finereader the same way? For example, in the two scripts I posted below, the app is not referred to as ‘ABBYY FineReader for ScanSnap’. I think that the script may be trying to ‘connect’ to something finereader-related which doesn’t exist on your system.

That said, it looks like ‘busy’ is available in your version. Where did you call the handler in your script?

Hi Mockman
Thank you for your interest. Here is the script which I tried. It does OCR the document, but wud not close. As it throws error as documents before, it blocks the subsequent workflow.
Cheers

tell application "Finder" to set theFile to selection as alias

tell application "ABBYY FineReader for ScanSnap"
	my WaitWhileBusy() -- tried version where it called the handler here & without →same error
	recognize theFile and export to theFile as pdf with silent mode
	delay 1
	tell me to WaitWhileBusy(). -- If I dont call this handler here, Abbyy quits
	quit
end tell
on WaitWhileBusy()
	repeat while IsMainApplicationBusy()
	end repeat
end WaitWhileBusy

on IsMainApplicationBusy()
	tell application "ABBYY FineReader for ScanSnap"
		set resultBoolean to busy
	end tell
	return resultBoolean
end IsMainApplicationBusy

That’s difficult for me to mimic exactly as the dictionary on my version is very different (doesn’t have recognize, for example).

All I can suggest is grasping at straws… is there a ‘close’ command? Maybe try ‘close document 1 without saving’. There should be some ‘standard’ commands (e.g. close, quit).

What happens if you ‘delay 5’ immediately prior to attempting to quit/close (if it helps then reduce until it stops helping).

Worst case scenario, send an email to Abbyy’s tech support.

Thanks Mockman, Besides Abby scansnap suite I posted above, rest of standard suite is below, nothing else exists

Just wondering if cud be done with shell
something like

I think -w makes it wait till the application quits
I’ll give this a try with a large file, usually 1-2 mb files are not an issue
Cheers

Well, it looks like you have to go with the ‘quit’ command.

It might be wise to use the path to the app in the shell command, e.g.

open -W -a ‘/Applications/FineReader.app’ /Users/username/Desktop/abc663366.png

Of course, you could put that in an applescript as well.

First, find quoted form of Posix path of ABBYY FineReader for ScanSnap.app
Then, put this quoted path in the Mockman’s script instead of quoted form of posix path of ABBYY FineReader.app.


set quotedAppPosixPath to quoted form of (POSIX path of (path to application "ABBYY FineReader for ScanSnap"))
set quotedImagePosixPath to quoted form of (POSIX path of (choose file of type "public.image"))

do shell script "open -w -a " & quotedAppPosixPath & space & quotedImagePosixPath

NOTES: 1) can’t test myself. 2) if this approach doesn’t work, then you can always use do script of Terminal.app for scanning. Because checking busy status of Terminal window is easy.