Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can apple scripts make an excel sheet that contains data from a folder?

So I have to pull documents from websites daily and I have to then create a report for it in excel

for eg - this is my folder


I need the name and some other metadata like Pixel Height

Pixel Width of every jpeg in the folder in an excel file

so how do i make that possible?

Thank you in advance

MacBook Air 13″, macOS 12.2

Posted on Mar 12, 2022 1:31 PM

Reply
Question marked as Best reply

Posted on Mar 16, 2022 9:53 AM

Looking at your script, it does work - at least, it does exactly what you ask it to.


It might not do what you want, but that's a different issue :)


Specifically, there's nothing in your script that iterates through the files in the folder, so that's the first change you need.


In addition, you need to extract metadata related to the image files, and that's actually easier via shell commands, so I'd incorporate that into the script.


Finally (for now), it's actually easier to write to a tab-delimtied text file and then open that in Excel. Much easier than trying to manipulate Excel directly.


Here's a script that will do that. It first asks for a folder, then grabs a list of JPEG images in that folder.

Then it iterates through that list, extracting the file name, image height and image width data, writing it out to a text file.

At the end of the script it instructs Excel to open that text file, which Excel does, automatically translating tab-delimited text into columns.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- prompt the user for the folder to process
-- this could be fixed if you prefer
set theFolder to choose folder

-- define the name of the output file to create
set outputFilename to "images.txt"
-- and make a new file
set theOutputFile to makeNewFile(outputFilename)

-- write a header column to the file
my writeToFile(theOutputFile, "Name" & tab & "Height" & tab & "Width")

-- ask the Finder...
tell application "Finder"
	-- to get a list of all the JPEG images in the folder
	set theFiles to (every file of folder theFolder whose kind is "JPEG image") as alias list
	-- for each file...
	repeat with eachFile in theFiles
		-- get its POSIX path (needed for the shell script)
		set qp to quoted form of POSIX path of eachFile
		-- extract the file name
		set fn to name of eachFile
		-- use a shell script to get the image height
		set imageHeight to last word of (do shell script "mdls -name kMDItemPixelHeight " & qp)
		-- use another shell script to get the image width
		set imageWIdth to last word of (do shell script "mdls -name kMDItemPixelWidth " & qp)
		-- and write that data to the file
		my writeToFile(theOutputFile, fn & tab & imageHeight & tab & imageWIdth)
	end repeat
end tell

-- once we're done, close the file
close access theOutputFile
-- and tell Excel to open it
tell application "Microsoft Excel" to open ((get path to desktop) & outputFilename as text)

on makeNewFile(theFileName)
	-- this defines a new file
	set theFile to open for access (file ((get path to desktop) & theFileName as text)) with write permission
	-- clears out any existing data
	set eof theFile to 0
	-- and passes it back
	return theFile
end makeNewFile

on writeToFile(theFile, theText)
	-- simple append-write to the file
	write theText & return to theFile
end writeToFile





Similar questions

5 replies
Question marked as Best reply

Mar 16, 2022 9:53 AM in response to Ash8420

Looking at your script, it does work - at least, it does exactly what you ask it to.


It might not do what you want, but that's a different issue :)


Specifically, there's nothing in your script that iterates through the files in the folder, so that's the first change you need.


In addition, you need to extract metadata related to the image files, and that's actually easier via shell commands, so I'd incorporate that into the script.


Finally (for now), it's actually easier to write to a tab-delimtied text file and then open that in Excel. Much easier than trying to manipulate Excel directly.


Here's a script that will do that. It first asks for a folder, then grabs a list of JPEG images in that folder.

Then it iterates through that list, extracting the file name, image height and image width data, writing it out to a text file.

At the end of the script it instructs Excel to open that text file, which Excel does, automatically translating tab-delimited text into columns.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- prompt the user for the folder to process
-- this could be fixed if you prefer
set theFolder to choose folder

-- define the name of the output file to create
set outputFilename to "images.txt"
-- and make a new file
set theOutputFile to makeNewFile(outputFilename)

-- write a header column to the file
my writeToFile(theOutputFile, "Name" & tab & "Height" & tab & "Width")

-- ask the Finder...
tell application "Finder"
	-- to get a list of all the JPEG images in the folder
	set theFiles to (every file of folder theFolder whose kind is "JPEG image") as alias list
	-- for each file...
	repeat with eachFile in theFiles
		-- get its POSIX path (needed for the shell script)
		set qp to quoted form of POSIX path of eachFile
		-- extract the file name
		set fn to name of eachFile
		-- use a shell script to get the image height
		set imageHeight to last word of (do shell script "mdls -name kMDItemPixelHeight " & qp)
		-- use another shell script to get the image width
		set imageWIdth to last word of (do shell script "mdls -name kMDItemPixelWidth " & qp)
		-- and write that data to the file
		my writeToFile(theOutputFile, fn & tab & imageHeight & tab & imageWIdth)
	end repeat
end tell

-- once we're done, close the file
close access theOutputFile
-- and tell Excel to open it
tell application "Microsoft Excel" to open ((get path to desktop) & outputFilename as text)

on makeNewFile(theFileName)
	-- this defines a new file
	set theFile to open for access (file ((get path to desktop) & theFileName as text)) with write permission
	-- clears out any existing data
	set eof theFile to 0
	-- and passes it back
	return theFile
end makeNewFile

on writeToFile(theFile, theText)
	-- simple append-write to the file
	write theText & return to theFile
end writeToFile





Mar 12, 2022 1:39 PM in response to Ash8420

set num to 2
set imgfolder to "/Users/vanshshah/Desktop"
set imgName to "/Users/vanshshah/Desktop/imgFolder"
repeat with img in imgfolder
	tell application "Microsoft Excel"
		tell row num
			set value of cell 1 to imgName
		end tell
	end tell
	set num to num + 1
end repeat

this is what i tried doing but it didnt work it gave me the following excel file (Also i am very new to apple scripts)


Mar 14, 2022 9:46 PM in response to Ash8420

I suggest using one of these tools:

https://apple.stackexchange.com/questions/222342/terminal-command-to-get-all-of-a-files-metadata


https://exiftool.org/


I have used the exiftool. You have to download exiftool


mac $ exiftool -s -ImageSize /Users/mac/Desktop/all\ Screen\ Shots/Screen\ Shot\ 2022-03-14\ at\ 3.03.11\ PM.png 
ImageSize                       : 1920x1080
mac $ 


How to run a unix command and get back the data.

(* 


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.




	Author: rccharles
	
	For testing, run in the Script Editor.
	  1) Click on the Event Log tab to see the output from the log statement
	  2) Click on Run
	  
	For running shell commands see:
	https://developer.apple.com/library/archive/technotes/tn2065/_index.html
	http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
	




 *)




on run
	-- Write a message into the event log.
	log "  --- Starting on " & ((current date) as string) & " --- "
	--  debug lines
	
	
	set desktopPath to path to home folder
	set unixDesktopPath to POSIX path of desktopPath
	log "unixDesktopPath = " & unixDesktopPath
	
	set quotedUnixDesktopPath to quoted form of unixDesktopPath
	log "quoted form is " & quotedUnixDesktopPath
	
	try
		set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath
		display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
	on error errMsg
		log "ls -l error..." & errMsg
	end try
	
end run


Can apple scripts make an excel sheet that contains data from a folder?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.