I have Excel file that I need to input the results of an applescript. The Excel file have a column called PDF file count.
For now I’m interested to figure it out how to be able to choose various folders in our server, get the name of the folder & have a .pdf file count of each one of the folders chosen.
For example:
Volume name: Press
Folder 1 have 24 pdf files
Foder 2 have 67 pdf files
Folder 3 have 110 pdf files
This is the script I have that gives me a file a count of 1 chosen folder.
Tell application "Finder"
set chosen_folder to choose folder with prompt "Choose a folder or volume:" with multiple selections allowed
set folder_name to name of chosen_folder
count (every file in folder chosen_folder whose name extension is "pdf")
set file_total to result
end tell
I’m getting an AS error "Can’t get name of {alias …
Can anyone can advise how to proceed?
Model: iMac intel 10.6.8
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.7)
When Choose Folder has multiple selections allowed, a list is returned, not an alias.
tell application "Finder"
set chosen_folder to choose folder with prompt "Choose a folder or volume:" with multiple selections allowed
set folder_name to name of (item 1 of chosen_folder)
count (every file in folder (item 1 of chosen_folder) whose name extension is "pdf")
set file_total to result
end tell
Thanks mikerickson for your help. I’m able to choose multiple folders but when I run the script I’m getting the file count of 1 folder instead of all the folders selected. For example if i chose Folder 1 which contains 24 pdfs & Folder 2 which has 67 pdf files the results are 24 pdfs when I run the script. I would like a file count of 24 pdf for Folder 1 & 67 for Folder 2.
Do I need to use a repeat loop of all folders selected? Sorry I’m trying to teach myself with books
I am teaching myself without books
I’d go with a loop.
This is what I have so far. Basically you choose a folder with PDfs, get file count of PDfs on chosen folders, write the results in text file, open the text file in Excel.
The reason why Im using a text file to get the results of the PDF file count is because I dont how to do that in excel. It was easier for me to get results in a text file. Any suggestions?
--This script will count all PDFs in the chosen folders
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" as string with multiple selections allowed without invisibles
set results to ""
repeat with i from 1 to (count target_folder)
set thisFolder to (POSIX path of item i of target_folder)
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf | wc -l"
set results to (results & "" & thisFolder & ":" & fileCount & return)
--Creates a Text File on the Desktop with the results
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
set eof of theFile to 0
write results to file theFilePath
close access theFile
on error
close access theFile
end try
end repeat
--Will open the the PDF File Count.txt in Excel
tell application "Microsoft Excel"
activate
open text file filename "PDF File Count.txt"
end tell
Now i started to have an error :
“File permission error.” number -54 from file “Macintosh HD:Users:user:Desktop:PDF File Count.txt”
The only I was able to get rid of it was restarting the computer. Do I need to restart every time I have this error?
write the text to the file descriptor
.
write results to theFile
.
Thanks StefanK for your reply.
I have another issue. When I started this script all I cared about was the number of PDFs per chosen folder but I have been asked know if there any PDFs file names that starts with the letter R (meaning resend) I need to be able to put them in a separate column or line.
lets say that:
Folder 1 have 24 pdf files. From the 24 pdfs 3 of the PDF file names starts with R_ or R-. Total is now 21 because 3 are resends
Folder 2 have 67 pdf files 10 of them starts with R_ or R-. Total 57 PDFs 10 are resends
I tried using conditional statements but my efforts to script this part have not been successful. Any ideas, Please below script
--This script will count all PDFs in the chosen folders
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""
--Get pdf file count per folder & repeats
repeat with i from 1 to (count target_folder)
set thisFolder to (POSIX path of item i of target_folder)
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf | wc -l"
set results to (results & "" & thisFolder & ":" & fileCount & tab & return)
--This the part I need help
(*if PDF_fileName of target_folder starts with "r_" or "r-" then
--add another line or tab with the resends. I don't know something like folder1 have 3 resends from 24 pdfs.
end if*)
--Creates a Text File on the Desktop with the results
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
set eof of theFile to 0
write results to the file theFilePath
close access theFile
on error
close access theFile
end try
end repeat
--Will open the the PDF File Count.txt in Excel
tell application "Microsoft Excel"
activate
set display alerts to false -- 2004 dictionary says it only applies to VB macros, but it actually supresses most dialogs while running scripts
open text file filename "PDF File Count.txt" with tab and consecutive delimiter
end tell
the shell script line
do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[-_]*.pdf'"
filters all files starting with R-, R_, r- or r_
Thanks StefanK. I have 2 questions
" -type f -iname ‘R[-_]*.pdf’"
Will find any combination starting with R-, R_, r- or r_ but If I want to find R and any digit_ or -
Example:
r2-S6.05_A_002.pdf
r3_S6.05_A_003.pdf
r_S6.05_A_004.pdf
r-S6.05_A_006.pdf
Can I in the same expression search for all files that ends with .pdf have the total of files minus the ones that starts R_ , R- or R any digit
Something like:
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -iname -not 'R[-_]*.pdf' | wc -l"
Example:
r3_S6.05_A_003.pdf
r_S6.05_A_004.pdf
r_3_S6.05_A_006.pdf
S6.05_A_006.pdf
S6.05_A_008.pdf
S6.05_A_0010.pdf
The results should be
total of pdfs 3
total of resends (files thats starts with r-, R_ or R any digit) 2
You have been very helpful
This searches for R followed by hyphen, underscore or any digit
'R[0-9-_]*.pdf'
to get both results total and resend you need a second find line
or you filter first all PDF and get the resends with grep or awk.
The counting could also be accomplished without wc using
count (paragraphs of (do shell script "find.))
Hello again,
Another question. How can strip part of the file path results:
Results are:
/Volumes/PREPRESS/1_CATALOG/2_Press_Holding/PRINT_Catalog/2012/¢CUSTO_4/BODY/: 65
/Volumes/PREPRESS/1_CATALOG/2_Press_Holding/PRINT_Catalog/2012/¢CUSTO_4/BODY/: RESENDS 0
/Volumes/PREPRESS/1_CATALOG/2_Press_Holding/PRINT_Catalog/2012/¢CUSTO_4/COVERS/: 23
/Volumes/PREPRESS/1_CATALOG/2_Press_Holding/PRINT_Catalog/2012/¢CUSTO_4/COVERS/: RESENDS 6
I want to strip everything before the bullet ¢ then for every / and : a column. Something like this:
CUSTO_4 BODY 65
CUSTO_4 BODY RESENDS 0
CUSTO_4 COVERS 23
CUSTO_4 COVERS RESENDS 6
I tried using text item delimiters but I don’t know how to implemented.
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""
repeat with i from 1 to (count target_folder)
set thisFolder to (POSIX path of item i of target_folder)
--Find & count all PDFs in folders selected that DON'T starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
set results to (results & "" & thisFolder & ":" & fileCount & return)
--Find & count all PDFs in folders selected that starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
set results to (results & "" & thisFolder & ":" & tab & tab & "RESENDS" & fileCount & return)
end repeat
--write results to a txt file
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
set eof of theFile to 0
write results to file theFilePath
close access theFile
on error
close access theFile
end try
--Open the txt file in Excel
Will open the the PDF File Count.txt in Excel
tell application "Microsoft Excel"
activate
set display alerts to false -- 2004 dictionary says it only applies to VB macros, but it actually supresses most dialogs while running scripts
set screen updating to true -- By setting this to false you can greatly improve speed
open text file filename "PDF File Count.txt"
end tell
Thanks in advance
Ok after a long search & reading I have the script almost working the way I want. I decided to use do shell script because I can delimiter & achieve what I need without using Text item delimiters which requires more code.
I have seen this script so many times that i need another guru eyes to search where I’m making the mistake. In the sections that says:
I’m getting the total PDF file count on this section:
-Find & count all PDFs in the folders selected that DON'T starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
-- Delimeter results before ¢ the bullet and convert each / to a tab
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o ¢.* | sed -e 's/¢\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
-->Results
set results to (results & "" & thisFolder & fileCount & return)
When I running this section I’m NOT getting the total file PDF count for the files that starts with letter “R” which are RESENDS
--Find & count all PDFs in the folders selected that starts with letter R (RESENDS)
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
-- Delimeter results before ¢ the bullet and convert each / to a tab
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o ¢.* | sed -e 's/¢\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
-->Results
set results to (results & "" & thisFolder & "RESENDS" & fileCount & return)
If I delete the line
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o ¢.* | sed -e 's/¢\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
I get the results I want but then I have the entire file path which I don’t need. What am i missing in the grep expression?
Here is the entire script:
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""
repeat with i from 1 to (count target_folder)
set thisFolder to (POSIX path of item i of target_folder)
--Find & count all PDFs in the folders selected that DON'T starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
-- Delimeter results before ¢ the bullet and convert each / to a tab
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o ¢.* | sed -e 's/¢\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
-->Results
set results to (results & "" & thisFolder & fileCount & return)
------------------------
--Find & count all PDFs in the folders selected that starts with letter R (RESENDS)
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
-- Delimeter results before ¢ the bullet and convert each / to a tab
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o ¢.* | sed -e 's/¢\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
-->Results
set results to (results & "" & thisFolder & "RESENDS" & fileCount & return)
end repeat
--For testing display dialog results
----------------------------------------
--write results to "PDF File Count.txt" file
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
set eof of theFile to 0
write results to theFile
close access theFile
on error
close access theFile
end try
----------------------------------------
--Open the "PDF File Count.txt" file in Excel
tell application "Microsoft Excel"
activate
set display alerts to false -- 2004 dictionary says it only applies to VB macros, but it actually supresses most dialogs while running scripts
set screen updating to true -- By setting this to false you can greatly improve speed
open text file filename "PDF File Count.txt"
end tell
Thank you in advance if U can help me