Running Scans from Scripts

Moderator: Kathy_9

Post Reply
LeviFiction
Advisor
Posts: 5984
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Running Scans from Scripts

Post by LeviFiction »

JoeB asked the question can we use scripts to call scanning software outside of the normal TWAIN and WIA protocols. The answer, is yes. It would essentially be very close in execution to his NIK Scripts. It's just starting the external software, and then grabbing the resulting files.

How you do this depends on the software, but in general the procedure would be as follows. We assume since we're scanning any scans will become new images, though we can just as easily assume we want them to be new layers. For simplicity we'll use new images.

Open Scanner software
Manually scan images to a pre-set folder
Close scanner software and continue the script execution
Find all new images added to the pre-set folder
Open those images as new images inside PSP.

PSP also has an FileOpenAsNewLayer command you can use to open all of the images as layers in the current image if you prefer.

If the scanning software supports command-line it can be setup to specifically scan directly to a certain folder without manual input.
JoeB
Posts: 2662
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Running Scans from Scripts

Post by JoeB »

EDIT: For those interested and following this thread, @LeviFiction solved the issue with a simple script which is near the bottom of the thread! My post below this edit explains what I was trying to achieve. TWAIN scanning software (which has advanced features for specific scanners/printers) is only 32bit so can't be invoked using the Import From Scanner options in 64bit PSP. Only the WIA interface, much more limited, is available.

But I assume most printer/scanners not only have 32bit TWAIN drivers but standalone scanning utility software in the form of an executable (.exe) file and which, when opened, gives immediate access to the scanner driver itself. If such is the case for you, and that software has the option of sending a scan directly to a program like PSP, then Levi's script, bound to a button and placed next to the Scanner button on your toolbar, would open that scanner software from your 64bit PSP. No need to open a 32bit version of PSP to use that 32bit TWAIN scanner interface. Just a heads-up FYI for anyone interested!.

EDIT 2: I have also realized that using this script as a button on my toolbar to open my Font Manager from PSP when needing fonts is much more convenient than having to open it from elsewhere.

Below is my original reply to Levi's post above.
-------------------------------------------------------------------

Yes, I figured that something along the lines of the NIK script would be needed. And I actually tried to edit one that might do the trick but my knowledge of Python was to limited to make it work. That's because I couldn't easily figure out how to edit it to just open the the Scan Utility executable and disappear - the script, that is, not the Scan Gear Utility. :)

And yes again,, it's a new image I'm looking for, now as a layer (which was causing problems with my attempts to edit my Nik script.

What I'm looking for is a simple script that just opens the Scan Utility so that I can then choose the Scan Gear Twain option. Twain isn't an executable to my knowledge so I can't just open it but have to go through the Utility interface. If PSP 64 bit is already open, and I can open the Scan Utility, then that interface should appear above PSP. Then I can select Scan Gear (the Twain driver), do the scan and - because of my settings in Scan Gear - the scanned image will be sent to my open PSP workspace. I'm not sure if Scan Gear disappears at that time but at the very least it becomes hidden behind PSP. And I think there might be a setting in Scan Gear to make it close after a scan. But that's not as important as just being able to open it and scan in the first place. So it's the script I could use. Thanks in advance!
Last edited by JoeB on Thu Mar 18, 2021 7:08 pm, edited 4 times in total.
Regards,

JoeB
Using PSP 2019 64bit
LeviFiction
Advisor
Posts: 5984
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: Running Scans from Scripts

Post by LeviFiction »

In the Nik script change "subprocess.call" to "subprocess.Popen" in the Nik script. It should call your Scan Utility executable and then the script continues until it's finished executing. Call is used to wait for a response from the program you started, Popen doesn't wait.

I haven't tested it as I don't have a scanning utilty on my computer and I'm unsure if there is any nuance in how the script executes using Popen vs Call. But that should usually be all you need to do.
JoeB
Posts: 2662
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Running Scans from Scripts

Post by JoeB »

OK, I did change the subprocess to Popen as you suggested. However, it didn't actually cause the script to run to the end without stopping, and when running the script I got script errors because of the different events that the script expects to happen such as having an open image to start with, copying the image, etc.

After much experimenting with various edits I finally got to a minimal script, which I have attached, and which works just fine. By that I mean that running the script opens my Scan Utility interface (see attached image showing the interface) and I can click the Scan Gear option which opens my scanning Twain software with all of the advanced options. Pressing "Scan" on that interface also works as it's supposed to, the item is scanned and - because I have set the Twain software to open the scanned image in PSP 2019 64bit, the image appears as promised and the Twain interface disappears behind PSP. So far excellent, and no script errors using the attached, zipped script!

However, it now seems to me that I don't even need some of the stuff that I have still kept in the script. That's because the image that the scanner opens in the PSP workspace does NOT have the name of the temporary image that I have set in the script on line 29. Instead it has the same generic numbered image names that I get if I simply run the Scan Utility software by itself as a standalone program without using PSP or the script.

In other words, the part of the script that normally sends a temp file to my Documents folder is not needed because the script isn't actually either saving the scanned image into documents or, of course, subsequently retrieving it from there. The scanner software is just doing what it normally does - i.e., scanning an image with the settings I have set in that software and sending it to the program I set in that software, which is my open copy of PSP 64bit.

So I thought I could also delete lines 22 through 29 as well as lines 43 and 44. But then the script doesn't even open the Scan Utility and throws the following error:

Executing RunScript
Using Filter: C:\Program Files (x86)\Canon\IJ Scan Utility\SCANUTILITY.exe
Traceback (most recent call last):
File "<string>", line 33, in Do
NameError: global name 'args' is not defined

Script 'ScannerTwain_Short_3' has completed with an error.


So while the attached script definitely opens my Scan Utility software basically as a standalone program which works as advertised, my curiousity has me asking if there is a further modification that can be made so that the script ONLY contains the necessary commands that opens that software and doesn't need to contain any other, non-essential commands that aren't actually being used or implemented anyway when the script is run.

Thanks for any help!
Attachments
ScannerTwain_Short.zip
(970 Bytes) Downloaded 10 times
Scanner Utility Interface
Scanner Utility Interface
Regards,

JoeB
Using PSP 2019 64bit
LeviFiction
Advisor
Posts: 5984
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: Running Scans from Scripts

Post by LeviFiction »

VSCode detects no errors with the script, and it looks alright to me. I had to replace your Scan utility with Notepad but this is the code I have after removing unnecessary portions of it.

Code: Select all

from PSPApp import *
import subprocess

def ScriptProperties():
    return {}

def Do(Environment):
    # Path to stand-alone app
    filterExe = 'c:\\windows\\system32\\notepad.exe'

    # Create args variable which is a list of arguments, in this case only the command
    # Pass to Popen, script should end after calling Popen
    args = [filterExe]
    subprocess.Popen(args, shell=True)
This ran perfectly, no errors about the args variable. So not sure what that was about.
JoeB
Posts: 2662
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Running Scans from Scripts

Post by JoeB »

Thanks! I believe I know what that particular issue was because, as I said, I had also deleted Line 44, which contained the arguments being passed to Popen, so no standalone app to open. And, if I recall correctly, I might previously have tried not deleting that line but, while the script ran, Line 44 also still contained the tmpFilename argument which would likely still have thrown a script error given that there was no tmpFilename.

So I am very grateful for your help with this! I now have a simple script that can simply open any program from PSP. It's extremely useful in this case to be able to use my TWAIN software without having to open a 32bit PSP version, and works just the same because that software already has the capability/option of sending the scan directly into the PSP workspace.

I have no idea if there would be any other standalone program that I might want to use that had the option of sending the output to PSP (probably not) but if I should run across one this little script would be great!

Thanks again!
Regards,

JoeB
Using PSP 2019 64bit
JoeB
Posts: 2662
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Running Scans from Scripts

Post by JoeB »

NOTE: I have edited my first post at the start of this thread to make it clear to those new to the thread that Levi's short script above does exactly what I was looking for.

EDIT: Despite the bracketed comment I made in the last sentence of my post above this one, I have actually found another program that I want to open from within PSP using this script, and that's my Font Manager. I've put it as a button on my toolbar just beneath the Text tool so that when I'm dealing with text in PSP the font manager is easily available to open at the same time. I'm all in for convenience!
Regards,

JoeB
Using PSP 2019 64bit
Post Reply