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"
    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.

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

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:


<?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">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.