I think have messed up with paths, please help / suggest better method
I intend to move 1 file (theFile) at a time (in sequence, oldest file) to MergeFolder (so I can keep an eye), where it would be merged with a Master File (at some other location). Once merged, theFile would be moved out of Mergefolder( may be to recycle bin)
Cheers
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "Quartz"
set ScannedFolder to "Catalina:Users:one:Desktop:DesktopScan:"
set MergeFolder to "Catalina:Users:one:Desktop:1:"
set firstFile to "Catalina:Users:one:Desktop:MasterFile.pdf"
tell application "Finder"
set scannedFiles to (every file of (alias ScannedFolder))
repeat until ((count of scannedFiles) is equal to 0)
set mergeFile to (every file of (alias MergeFolder))
if (count of mergeFile) is 0 then
-- Folder is "Empty"
set theFile to POSIX file (mostRecentlyModifiedIemIn(ScannedFolder, true) of me) as alias
-- Needs HFS path
move theFile to MergeFolder
-- now append PDF
AppendPDFfile(firstFile, theFile) of me
-- move file out of mergefolder
move theFile to desktop
else
-- Folder not empty
set mergeFile to (every file of (alias MergeFolder)) --update
repeat until ((count of mergeFile) is 0)
idle of me
end repeat
end if
end repeat
end tell
on idle
return 3
end idle
----------------------------------------------------------------
-- https://macscripter.net/viewtopic.php?id=44170
on mostRecentlyModifiedIemIn(folderPosixPath, sortTypeAscending)
set theNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of folderPosixPath) -- make URL for folder because that's what's needed
set theNSFileManager to current application's NSFileManager's defaultManager() -- get file manager
-- get contents of the folder
set keysToRequest to {current application's NSURLPathKey, current application's NSURLContentModificationDateKey} -- keys for values we want for each item
set theURLs to theNSFileManager's contentsOfDirectoryAtURL:theNSURL includingPropertiesForKeys:keysToRequest options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
-- get mod dates and paths for URLs
set theInfoNSMutableArray to current application's NSMutableArray's array() -- array to store new values in
repeat with i from 1 to theURLs's |count|()
set anNSURL to (theURLs's objectAtIndex:(i - 1)) -- zero-based
(theInfoNSMutableArray's addObject:(anNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value))) -- get values dictionary and add to array
end repeat
-- sort them in date order
set theNSSortDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLContentModificationDateKey) ascending:sortTypeAscending -- describes the sort to perform Change for ascending/ descending type
theInfoNSMutableArray's sortUsingDescriptors:{theNSSortDescriptor} -- do the sort
-- get the path of the first item
return ((theInfoNSMutableArray's objectAtIndex:0)'s valueForKey:(current application's NSURLPathKey)) as text -- extract path and convert to text
end mostRecentlyModifiedIemIn
-- https://macscripter.net/viewtopic.php?id=22558
# Set Master, combine the 2nd PDF at end of Master
on AppendPDFfile(firstFile, theFile)
-- Deal with Master PDF aka firstFile
set firstFile to current application's class "NSURL"'s fileURLWithPath:(POSIX path of firstFile)
set firstDoc to current application's PDFDocument's alloc()'s initWithURL:firstFile
set firstDocCount to firstDoc's pageCount()
-- Deal with pdf to merge with Master aka theFile
set theFile to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFile))
set aDoc to (current application's PDFDocument's alloc()'s initWithURL:theFile)
set aDocCount to aDoc's pageCount()
-- Now Append one page at a time
repeat with i from 1 to aDocCount
set thePDFPage to (aDoc's pageAtIndex:(i - 1))
-- Append Page
(firstDoc's insertPage:thePDFPage atIndex:firstDocCount)
-- Increase the Count of Master pdf as 1 page has been added
set firstDocCount to firstDocCount + 1
end repeat
-- Catch Error
try
# OverWrite the MASTER
(firstDoc's writeToURL:firstFile)
on error
display dialog "Append failed"
end try
end AppendPDFfile
You have 2 loops where you test a condition. And those conditions never change inside their respective loops, so your script either terminates immediately, or runs forever.
Generally:
repeat until whatever
# do stuff
# UPDATE whatever
end repeat
You could help us help you by outlining the actual problems you experience. Just a tip.
One208. I’ve included my suggestion below, which works as expected in my testing. I do not understand what is supposed to happen if MergeFolder contains files, so the script displays a dialog instead. If a delay is desired that is easily added to the script where you want it to occur.
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "Quartz"
set ScannedFolder to "Catalina:Users:one:Desktop:DesktopScan:"
set MergeFolder to "Catalina:Users:one:Desktop:1:"
set firstFile to "Catalina:Users:one:Desktop:MasterFile.pdf"
tell application "Finder"
set scannedFiles to every file of folder ScannedFolder sort by modification date
set mergedFiles to every file of folder MergeFolder
if (count mergedFiles) = 0 then
repeat with i from 1 to (count scannedFiles)
set aFile to item i of scannedFiles
set theMovedFile to ((move aFile to folder MergeFolder) as text)
AppendPDFfile(firstFile, theMovedFile) of me
move file theMovedFile to desktop
end repeat
else
display dialog "The merge folder is not empty" buttons "OK" cancel button 1 default button 1
end if
end tell
on AppendPDFfile(firstFile, theFile)
-- Deal with Master PDF aka firstFile
set firstFile to current application's class "NSURL"'s fileURLWithPath:(POSIX path of firstFile)
set firstDoc to current application's PDFDocument's alloc()'s initWithURL:firstFile
set firstDocCount to firstDoc's pageCount()
-- Deal with pdf to merge with Master aka theFile
set theFile to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFile))
set aDoc to (current application's PDFDocument's alloc()'s initWithURL:theFile)
set aDocCount to aDoc's pageCount()
-- Now Append one page at a time
repeat with i from 1 to aDocCount
set thePDFPage to (aDoc's pageAtIndex:(i - 1))
-- Append Page
(firstDoc's insertPage:thePDFPage atIndex:firstDocCount)
-- Increase the Count of Master pdf as 1 page has been added
set firstDocCount to firstDocCount + 1
end repeat
-- Catch Error
try
# OverWrite the MASTER
(firstDoc's writeToURL:firstFile)
on error
display dialog "Append failed"
end try
end AppendPDFfile
Hi peavine
Thank you for having a look & fixing it for me, works perfect
I am paronoid abt not appending the document to the correct master document
The idea of this script was to automate collating the documents. Say for eg utility bill. Get one every month, as I download(s), I move them to DesktopScan folder → from This folder-> moved one by one in correct sequence (date) to MergeFolder, where it appended to MasterUtility File. The repeat loop is process all files in DesktoScan, and stop when none left. I move the appended files to another folder, where I review & delete them.
Thank you for getting the basic script right for me. Next i wud work to select different MasterFile based on file name being moved in mergedFolder, that I thing wud be a perfect workflow
All the best & happy new year
Full script
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "Quartz"
set ScannedFolder to "Catalina:Users:one:Desktop:DesktopScan:"
set MergeFolder to "Catalina:Users:one:Desktop:1:"
set firstFile to "Catalina:Users:one:Desktop:MasterFile.pdf"
set moveOut to "Catalina:Users:one:Desktop:2:"
tell application "Finder"
set scannedFiles to every file of folder ScannedFolder sort by modification date
set mergedFiles to every file of folder MergeFolder
if (count mergedFiles) = 0 then
repeat with i from 1 to (count scannedFiles)
set aFile to item i of scannedFiles
set theMovedFile to ((move aFile to folder MergeFolder) as text)
AppendPDFfile(firstFile, theMovedFile) of me
move file theMovedFile to moveOut
end repeat
else
-- Folder not empty
set mergeFile to (every file of (alias MergeFolder)) --update
repeat until ((count of mergeFile) is 0)
idle of me
set mergeFile to (every file of (alias MergeFolder)) --update
end repeat
end if
end tell
on idle
return 3
end idle
on AppendPDFfile(firstFile, theFile)
-- Deal with Master PDF aka firstFile
set firstFile to current application's class "NSURL"'s fileURLWithPath:(POSIX path of firstFile)
set firstDoc to current application's PDFDocument's alloc()'s initWithURL:firstFile
set firstDocCount to firstDoc's pageCount()
-- Deal with pdf to merge with Master aka theFile
set theFile to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFile))
set aDoc to (current application's PDFDocument's alloc()'s initWithURL:theFile)
set aDocCount to aDoc's pageCount()
-- Now Append one page at a time
repeat with i from 1 to aDocCount
set thePDFPage to (aDoc's pageAtIndex:(i - 1))
-- Append Page
(firstDoc's insertPage:thePDFPage atIndex:firstDocCount)
-- Increase the Count of Master pdf as 1 page has been added
set firstDocCount to firstDocCount + 1
end repeat
-- Catch Error
try
# OverWrite the MASTER
(firstDoc's writeToURL:firstFile)
on error
display dialog "Append failed"
end try
end AppendPDFfile