1

Pbpaste doesn't seem to work as expected when I run it from a cron job.

I have an applescript that opens a PDF file in Preview and sends it the copy command and then closes Preview. Next I'd like to paste that into a file, pbpaste > pasta.txt. This all works great, when I run it as myself.

However if I put it as a task in crontab -e, it does run correctly and even created the output file, but the contents are empty. It's as if whatever context the running is happening in has a different concept of the clip board.

My crontab is like this:

* * * * * cd /Users/bemmu/Public;osascript yeah.script

And yeah.script is like this:

do shell script "open tiliote.pdf"
delay 2
tell application "Preview"
    activate
    delay 2
    tell application "System Events" to keystroke "a" using {command down}
    delay 2
    tell application "System Events" to keystroke "c" using {command down}
    delay 2
    tell application "System Events" to keystroke "q" using {command down}
    delay 2
end tell
do shell script "pbpaste > pasta.txt"

Expected result is that pasta.txt would contain the PDF file in text and that is what happens if I run this script by hand. However if cron runs it, pasta.txt gets created but contains nothing (size is 0).

I can see that the copy definitely is successful when run from cron, as I can paste it myself later. It's just that pbpaste doesn't seem to be working from cron.

1
  • What user does cron use for executing your script? Can you influence that?
    – Daniel Beck
    Dec 9, 2010 at 17:38

1 Answer 1

1

Why not ditch the script and use an Automator workflow (or compiled app) since they have a dedicated method for extracting text from PDFs into a text file.

enter image description here

You also might try ditching Cron and running your script via a LaunchAgent. Example:

~/Library/LaunchAgents/us.peelman.Terminal.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>us.peelman.TerminalLauncher</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal</string>
    </array>
</dict>
</plist>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .