Jump to content

Alfred PDF Tools – Optimize, encrypt and manipulate PDF files


Recommended Posts

  • 1 month later...
  • 2 weeks later...
  • 2 months later...

@deanishe

 

The Optimize action of this workflow executes a subprocess called k2pdfopt. While running, this subprocess outputs the progress of the operation (tipically informing the conclusion of each page of the PDF file). Look:

 

ZJYMM7t.png

 

Since the operation can take a long time in larger PDF files it would be very neat to show the progress of the operation in Alfred bar, maybe invoking a Script Filter or something that can inform how many pages were processed. Do you know if it's possible to retrieve the output of the subprocess for this purpose?

Edited by xilopaint
Link to comment

You can read the "SOURCE PAGE X of 4" lines by capturing the STDOUT/STDERR output of the process.

 

Here's a bit of code from a program I wrote that does something similar:

proc = subprocess.Popen(cmd,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

while True:
    if proc.poll() is not None:  # subprocess has finished
        break
    line = proc.stdout.readline().strip()
    if not line:
        continue

    # parse output here

 

Edited by deanishe
Link to comment

Thank you, @deanishe.

 

I had already read a similar solution here. My problem is my subprocess is executed from a Run Script object and I need the capability of invoking a Script Filter whenever I want to check the progress (what can happen multiple times in a single execution with large files) . How can I better achieve this? Should I write the output of the subprocess in a file and retrieve the data with the Script Filter or there is any better way?

Edited by xilopaint
Link to comment
6 hours ago, deanishe said:

Yup. Writing the status to a file is probably the best way.

 

Another possibility is to start a server for your Script Filter to fetch the status from, but that seems like overkill. 

 

I tried to use settings API of alfred-workflow and had some issues. In some tests the last line of the subprocess output was not added to settings.json file. Using "with statements" to write and open the files I had no issues. Is wf.settings somewhat slow or bad for using in loops?

 

This is working well:

proc = Popen(command, shell=True, stdout=PIPE)
page_number = 1

while proc.poll() is None:
    line = proc.stdout.readline()
    if "SOURCE PAGE" in line:
        with open(file_page_number, 'w') as f2:
            f2.write(str(page_number))
        page_number = page_number + 1

But this is not always working:

proc = Popen(command, shell=True, stdout=PIPE)
page_number = 1

while proc.poll() is None:
    line = proc.stdout.readline()
    if "SOURCE PAGE" in line:
        wf.settings['page_number'] = str(page_number)
        page_number = page_number + 1

I would like to use wf.settings if possible, but it seems unreliable in my tests. I should be missing something.

Edited by xilopaint
Link to comment

You shouldn't use the settings API because each Workflow instance assumes it's the only one accessing the settings.json file and will overwrite changes made by other instances/processes running at the same time.

 

It's a bad choice anyway: the settings are persistent data, meant to be kept as long as the workflow is installed. Such data very clearly belong in the cache directory. It's not as if Workflow.cache_data() is any harder to use.

Edited by deanishe
Link to comment
  • 3 weeks later...

Update (v2.12)

  • Fixed a critical bug in Split by File Size action that under certain circumstances created files in infinite loop.
  • Improved performance of Split by File Size and Optimize file actions.
  • Renames to suffix the environment variable in workflow’s configuration sheet.
Edited by xilopaint
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...