I’m writing a script that walks through a folder structure and returns the filename and page count of any PDF found, writing that into a text file.
In order to get the page count, I’m using mdls in a do shell script command.
The problem occurs when I’m setting the variable to be written to the text file. The script aborts at that point but without any error.
This is the line: set theText to this_item & tab & (do shell script shellCmd) & return
I’ve run in debug mode in Script Debugger and I’m not seeing anything when I execute that line which would indicate the problem.
A number of people here have contributed to what’s in this script through its predecessors, so there are sections that are much more advanced than my scripting skills might otherwise be…
If you could be so kind as to help me understand why the script is failing, I’d be grateful.
Thanks.
Cheers,
Jon
--
-- Created by: Jonathan Duke
-- Created on: 3/16/21
--
-- Copyright © 2021 Fitch Law Partners LLP, All Rights Reserved
--
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.2"
-- <https://www.macosxautomation.com/applescript/apps/FileManagerLib_stuff.zip>
use scripting additions
property NSPredicate : a reference to NSPredicate of current application
property NSSortDescriptor : a reference to NSSortDescriptor of current application
property kFileList : {}
set kFileList to {}
set shellCmd1 to "mdls -name kMDItemNumberOfPages -raw "
set overwriteExistingContent to false
--Code from EMail move script as an example of how to mix AS variables and shell script sections
--set shellCmd1 to "find "
--set shellCmd2 to " -type f -newermt "
--set shellCmd3 to "-01-01 ! -newermt "
--set shellCmd4 to "-12-31 -exec mv {} "
--set shellCmd5 to " \\;"
--
--set shellCmd to shellCmd1 & sourceDir & shellCmd2 & mailYear & shellCmd3 & mailYear & shellCmd4 & destDir & shellCmd5
--
--do shell script shellCmd with administrator privileges
--mdls -name kMDItemNumberOfPages -raw file.pdf
with timeout of 360 seconds
try
set theRootFolder to choose folder with prompt "Please select directory."
set theRootFolderPosixPath to POSIX path of theRootFolder
set URLArray to ¬
objects of theRootFolderPosixPath ¬
searching subfolders true ¬
include invisible items false ¬
include folders true ¬
include files true ¬
result type urls array
set PDFPred to NSPredicate's predicateWithFormat:("pathExtension ==[c] 'pdf'")
URLArray's filterUsingPredicate:PDFPred
set sortDescriptor to NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(true) selector:("localizedStandardCompare:")
URLArray's sortUsingDescriptors:({sortDescriptor})
set kFileList to URLArray as list
set theNewFilePath to choose file name with prompt "Save the PDF name and page count document as:"
set theFile to theNewFilePath
repeat with i in kFileList
set this_item to i as alias
set shellCmd to shellCmd1 & quoted form of (POSIX path of i)
set theText to this_item & tab & (do shell script shellCmd) & return --script fails here without any error being posted.
my writeTextToFile(theText, theFile, overwriteExistingContent) -- writeTextToFile(theText, theFile, overwriteExistingContent)
end repeat
end try
end timeout
-- A sub-routine for writing text to a file:
-- Courtesy of Apple
-- https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile