Ability to act on files in a folder one at a time

Asked by JoeBloggs

Hi,

Thanks in advance for any advice/pointers/comments!
I'm using Sikuli 2.05 and Java Runtime 1.8

I'm after a bit of advice/pointers on getting Sikuli to scan a folder and create a list of the contents and then be able to act on each file in turn.

Basically, I'm trying to create a 'drop folder' where files are placed in a folder and then Sikuli will scan the contents of the folder and if it sees 4 files will open each file in turn in a CAD package, run a script on each file (amending the file and then exporting in a different file format) and then close once finished.

I'm able to read the contents of the folder and see the files if I run the following:-
e.g

import os
dir="c:\\test"
for e in os.listdir(dir):
     filename=os.path.join(dir,e)
     print(filename)

This will show me the folder contents so I know Sikuli has found the folder and worked out what files are present:-
e.g

c:\test\test1.doc
c:\test\test2.doc

I just don't know the next steps to get it to then open each file it knows about.

I've tried:-

Import os
dir=”c:\testfolder”
for e in os.listdir(dir):
 filename=os.path.join(dir,e)
 type(‘o’,KEY_CTRL) # opens the location in the CAD software
 doubleClick(result) # Problematical line to get it to double click on each file
 click(export.png) # to just export the file from the software
 …. # other actions
 ….
 …..
Rest of script….

When ran this script will navigate to the drop folder, open one of the files in question, run through my processing and then, loop back to pick up another file. It will process the correct number of times. So, if there is 5 files in the 'drop folder' it runs through the 'loop' 5 times, before finishing. If 10 files in the folder it will run through the 'loop' 10 times.
But each time it goes to just one file and opens that. Not the named files that it knows about.

If I ask it to look to doubleClick(image.png) then again it will run through the correct number of loops but picks up a random file each time.
I understand that the line I'm using doubleclick(result) is probably wrong but don't know what I should be putting

Hope the above question makes sense. I'm not a heavy computer user or programmer just a CAD operator that has been tinkering with Sikuli for a few years and got it to do most of what I want, but struggle with programming/scripting as my brain doesn't work that way :-) So, please forgive me if this appears a dumb question or simplistic for you geniuses (bit of blatant flattery...)

Thanks again for any assistance.

Joe

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
JoeBloggs
Solved:
Last query:
Last reply:
Revision history for this message
Sean Bell (bell-sean-m) said :
#1

Hi Joe,

You probably want to look at using one of a couple other options to launch your CAD application from your script instead of "type" or "doubleClick".

If you already have your CAD application running, you can use App.focus("CAD_PROGRAM_NAME") to bring it up.

Better, if your CAD application will open files as an argument you can use:
myCadApp = openApp("Path_to_CAD\PROGRAM.exe " + filename)

You can use the above approach in your for loop to open each file and click the export buttons as needed.

Revision history for this message
JoeBloggs (joebloggs999) said (last edit ):
#2

Hi Sean,

Thank you very much for your input. Much appreciated!

I've been able to open files from Sikuli using scripts similar to:-

App.open("C:\\TESTFOLDER1\\testfile.txt")

I'm also figured out how to get Sikuli to scan a windows folder and report back what is the first file in that list, and print it out:-

import os

# Specify the directory path
dir="C:\TEST"

# List all files in the directory
files = os.listdir("C:\TEST")
res = [files[0]]
print[files[0]]

What I'm trying to do now is marry those two types of commands together so that the folder is scanned and it passes the res=[files[0]] to the open command so that it will open the first file in the list. Ideally, once opened I can run other commands and possibly delete the file before the script then runs again in a loop and moves onto the next file in the directory and then process that.

So, do you know how to pass the res=[files[0]] through to another command such as 'App.open' or doubleClick or something so that it launches.

I'm still trying and hopefully will figure it out if possible. Think I need to bite the bullet and do some proper python type training but any assistance you can provide would be greatfully received.

Thanks again.

Joe

Revision history for this message
JoeBloggs (joebloggs999) said :
#3

I think I've worked something out but not sure if this the best or most efficient way but it does what I want. So is a start :-)

Once I've created the 'list' of files in the directory, then the script pulls out the first entry in the list (as above) then need to convert it to a 'string' I think by using the following.

joe = map(str, [files[0]])

this makes the variable joe easier to work with (less brackets and stuff)
Then open the file using the default application by substituting it into the App.open command and joining it, like this:-

app.open("C:\\test\\" + "".join(joe))

This then opens the first file using the default app !

Just thought I'd put it here in case anyone else can use it or if anyone thinks I've done it wrong or over-complicated it and wants to tell me why.