Jump to content

Alfred PDF Tools – Optimize, encrypt and manipulate PDF files


Recommended Posts

  • 1 month later...
  • Replies 81
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Alfred PDF Tools A workflow for Alfred 3. Description Optimize, encrypt and manipulate PDF files. Download and Installation Download the workflow file from GitHub releases a

OMG! Love it!!!!!! TY so much <3

Oh nice, I actually just posted on the other workflows GitHub seeing if 2016 support was possible and he said no.   I agree that it isn't really a good fit for APT I was just naming features

Posted Images

  • 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 post

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 post

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 post
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 post

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 post
  • 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 post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...