Sunday, January 23, 2022

#1 2021-12-30 11:37:35 pm

One208
Member
Registered: 2021-03-15
Posts: 55

File Paths

Hi

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

Applescript:


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

Last edited by One208 (2021-12-31 06:39:11 am)


Filed under: , pdfappend

Offline

 

#2 2021-12-31 07:57:07 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 597

Re: File Paths

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:

Applescript:

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.

Offline

 

#3 2021-12-31 08:42:39 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1213

Re: File Paths

One208 wrote:

I think have messed up with paths, please help / suggest better method



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.

Applescript:

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

Last edited by peavine (2021-12-31 08:58:53 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2021-12-31 09:52:37 am

One208
Member
Registered: 2021-03-15
Posts: 55

Re: File Paths

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

Applescript:


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

Offline

 

#5 2022-01-03 12:57:54 am

One208
Member
Registered: 2021-03-15
Posts: 55

Re: File Paths

alastor933 wrote:

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:

Applescript:

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.


apologies, shud have posted the script, the loops were fine

Offline

 

#6 2022-01-03 01:00:43 am

One208
Member
Registered: 2021-03-15
Posts: 55

Re: File Paths

peavine wrote:


tell application "Finder"
    set scannedFiles to every file of folder ScannedFolder sort by modification date



Hi just wondering how do reverse the sorting (either oldest to newest or reverse)
Thank you for all the help

Offline

 

#7 2022-01-03 07:02:02 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1213

Re: File Paths

One208 wrote:

Hi just wondering how do reverse the sorting (either oldest to newest or reverse)
Thank you for all the help



You cannot change the sort order directly. One alternative is to modify the repeat loop--change existing line 1 below to line 2 below.

Applescript:

repeat with i from 1 to (count scannedFiles)
repeat with i from (count scannedFiles) to 1 by -1

An alternative is to use the reverse property. Under existing line 1 below, add new line 2 below.

Applescript:

set scannedFiles to every file of folder ScannedFolder sort by modification date
set scannedFiles to reverse of scannedFiles

Last edited by peavine (2022-01-03 07:32:13 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#8 2022-01-03 09:06:06 am

One208
Member
Registered: 2021-03-15
Posts: 55

Re: File Paths

Thanks peavine, this worked for me

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)