Groups | Search | Server Info | Login | Register


Groups > alt.comp.lang.applescript > #55

Re: AppleScript need: Can a file open at all

From Jolly Roger <jollyroger@pobox.com>
Newsgroups alt.comp.lang.applescript, apple.lists.applescript-users
Subject Re: AppleScript need: Can a file open at all
Date 2020-05-20 23:10 +0000
Organization People for the Ethical Treatment of Pirates
Message-ID <hilrmtF2cr3U1@mid.individual.net> (permalink)
References <0001HW.2470781D0010E5AF70000A0CA38F@SSL.usenetstorm.com> <hib1saFnc8hU1@mid.individual.net> <0001HW.2475D9ED00071A61700002EA138F@SSL.usenetstorm.com>

Cross-posted to 2 groups.

Show all headers | View raw


On 2020-05-20, Tim Murray <TimMurray@hotmail.com> wrote:
> On May 16, 2020, Jolly Roger wrote
> (in article <hib1saFnc8hU1@mid.individual.net>):
>
>> On 2020-05-16, Tim Murray<TimMurray@hotmail.com>  wrote:
>> > I recently made a gross error of formatting a disk that had good data on it.
>> > (My MBPro was stolen and when I got another, I ended up formatting my Time
>> > Machine disk. Aaarrrgh! Some computer tasks should not be performed with
>> > bourbon.) Anyway, I ran some recovery programs and got everything back, but
>> > there is one issue.
>> >
>> > My PDFs are all named File0001.pdf, File0002.pdf, and so on, but there are
>> > several hundred. Acrobat and Catalina’s Quick Look and Preview can deal
>> > with many of them.
>> >
>> > What I am looking for is a script to run through a folder and determine if a
>> > PDF can be opened or even if Quick Look works . . . I don’t need to
>> > actually do anything with it, just see if it can be opened. If it cannot,
>> > that’s okay: Trash it so that what remains are PDFs can be opened.
>> >
>> > In a way I don’t really need this per se, because I successfully retrieved
>> > most, if not all, the original files that made the PDFs, such as InDesign,
>> > Pages, Illustrator, and so on.
>> >
>> > So can someone here do that? Thanks.
>>
>> I've done this PDF document validation in Ruby using the pdf-reader Gem
>> before. Here's a simplified example script:
>>
>> ---
>> #!/usr/bin/env ruby
>>
>> require 'pdf-reader' # gem install 'pdf-reader'
>>
>> class Reader
>> def start
>> filepath = ARGV[0]
>> unless filepath.nil?
>> if File.exist?(filepath)
>> begin
>> reader = PDF::Reader.new(filepath)
>> puts "Document is valid: #{File.basename(filepath)}"
>> rescue StandardError =>  exception
>> puts "Couldn't open #{File.basename(filepath)}: #{exception.message}"
>> end
>> else
>> STDERR.puts "ERROR: Specified file does not exist: #{filepath}"
>> end
>> else
>> STDERR.puts "ERROR: You must provide the path to a PDF document as the first
>> command-line argument."
>> end
>> end
>> end
>>
>> reader = Reader.new()
>> reader.start()
>> ---
>>
>> Usage examples:
>>
>> ---
>> #./pdf_validator.rb ~/Documents/Hardware/Car\ Stereo/Old/KAC818.pdf
>> PDF seems valid: KAC818.pdf
>>
>> # ./pdf_validator.rb some_bad.pdf
>> PDF does not contain EOF marker: some_bad.pdf
>> ---
>>
>> You could certainly wrap this in an AppleScript "do shell script"
>> command to automate it if you wanted to. But I'd probably just modify
>> the script to do exactly what I want and run it on the command line and
>> be done with it.
>
> If I knew something about Ruby (other than just what is “is” I would work 
> with this, but I don’t. But thanks much.

You don't need to know much about Ruby. 

1. Copy the script above, paste it into a new text document, named
something like pdf_validator.rb. 

2. Add this to your ~/.zshrc or ~/.bashrc shell startup file to set the
GEM_HOME and GEM_PATH variables so that Ruby Gems are installed in your
home directory:

  # set Ruby gem paths

  export GEM_HOME=$(ls -t -U | ruby -e 'puts Gem.user_dir')
  export GEM_PATH=$GEM_HOME
  export PATH=$PATH:$GEM_HOME/bin

3. To make the GEM_HOME and GEM_PATH changes take effect, open a new
terminal window or enter this command from an existing terminal window: 

  source ~/.zshrc 

    -or- 

  source ~/.bashrc

4. Enter this command to validate that there is a path in your home
directory for Gems: gem env - you should see something like this:

  - GEM PATHS:
     - /Users/jr/.gem/ruby/2.6.0

5. Install the pdf-reader Gem with this command:

  gem install 'pdf-reader'

6. Test the script manually with something like:

  ./pdf_validator.rb some.pdf

7. If it works, then you can call the script from the AppleScript "do
shell script" command the same way you ran the script manually.

There are various command-line PDF tools you might be able to use
instead. But having never used one for the purpose of validation, I
can't advise you about them.

-- 
E-mail sent to this address may be devoured by my ravenous SPAM filter.
I often ignore posts from Google. Use a real news client instead.

JR

Back to alt.comp.lang.applescript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

AppleScript need: Can a file open at all Tim Murray <TimMurray@hotmail.com> - 2020-05-16 15:35 -0400
  Re: AppleScript need: Can a file open at all Jolly Roger <jollyroger@pobox.com> - 2020-05-16 20:48 +0000
    Re: AppleScript need: Can a file open at all Tim Murray <TimMurray@hotmail.com> - 2020-05-20 17:34 -0400
      Re: AppleScript need: Can a file open at all Jolly Roger <jollyroger@pobox.com> - 2020-05-20 23:10 +0000
        Re: AppleScript need: Can a file open at all Tim Murray <TimMurray@hotmail.com> - 2020-05-24 10:36 -0400

csiph-web