Copy File Info into Spreadsheet

hey all–

i’m completely new to applescript. i’m trying to create a database or spreadsheet of file information, and i want to be able to select a folder, and run some sort of macro to go through each file and copy the file name, the extension, and maybe the date created or modified into a text, excel, csv or database file. basically, i want to script a program to archive file contents.

it seems like it would be such a simple script to write for someone who knows what they are doing, but i have yet to find a satisfactory solution, except from microsoft, which provides a visual basic macro that doesn’t work.

please help, or give me something so i can get on the right track here! we have thousands of files to document, with more coming in daily, and we don’t have time to take down data for each one individually!

thanks

-b

AppleScript: 2.1.1
Browser: Firefox 2.0.0.4
Operating System: Mac OS X (10.4)

Welcome to the forum, BMM.

AppleScript is capable of writing directly to a text file, so the easy approach is to create a tab or comma delimited text file of your data and then you can open it in Excel or whatever else you choose.


set tFolder to choose folder
set del to tab -- the delimiter, could also be ","
-- using "entire contents" so the Finder will find file in folders within tFolder
set tData to "" -- placeholder for your data
tell application "Finder"
	set NF to name of tFolder
	set tFiles to files of entire contents of tFolder
	repeat with F in tFiles
		set {modification date:MD, name:N, name extension:NE} to F
		set tData to tData & N & del & NE & del & MD & return
	end repeat
end tell

set DF to open for access ((path to desktop folder as text) & "Folder_Data.txt") with write permission
try
	set eof of DF to 0 -- erases it, remove if you want to append.
	write tData to DF
	close access DF
on error e
	close access DF
	display dialog "Oops... " & e
end try

wow…thank you. script works great.

i’m trying to figure out some things, like how to set parameters for the variables. for instance, this line:

set {modification date:MD, name:N, name extension:NE, creation date:CD, size:FS} to F

works fine, but it’s giving me the file size in bytes (size in mb doesn’t work) and the date is given in its long format (Friday, June 15, 2007 1:46:15 PM instead of just 7/15/2007 13:46:15, for example). is there a way to set these parameters? if so what’s the easiest way?

thanks a lot for your help,

-b

If you don’t care about times, convert them:


set F to choose file
tell application "Finder" to set {modification date:MD, name:N, name extension:NE, creation date:CD, size:FS} to F
set MD to short date string of MD --> "3/19/05"
set CD to short date string of CD --> "3/19/05"
set FS to ((FS / 1024) div 1 as text) & "K" --> "134K"

If you do want time included, then it’s convenient to put it in sortable form: YY/MM/DD hh:mm


set F to choose file
tell application "Finder" to set {modification date:MD, name:N, name extension:NE, creation date:CD, size:FS} to F
set MD to FixDate(MD) --> "07/03/23 16:53"
set CD to FixDate(CD) --> "07/03/23 16:53"
set FS to ((FS / 1024) div 1 as text) & "K" --> "134K"

on FixDate(tDate)
	set myDelim to "/" -- add any delimiter character
	tell tDate
		tell (year * 10000 + (its month) * 100 + day) as string to text 3 thru 4 & myDelim & text 5 thru 6 & myDelim & text 7 thru 8 & space
		set sortableDate to result
		tell ((its hours) * 100 + (its minutes)) as string to text 1 thru 2 & ":" & text 3 thru 4
		return sortableDate & result
	end tell
end FixDate

is there a place where i can find all of the possible data i can pull on a file?

creation date
modification date
name
name extension
…what else is there?

FINALLY, and i promise this is the last thing, is it possible to get the duration of a movie file? i’m assuming it isn’t but just in case its a possibility.

thanks again for all your help

-b

Run this little script and you’ll see the properties available for the file you choose in the Script Editor Results pane.

set F to choose file
tell application "Finder" to set P to properties of F
P

This script will tell you what Spotlight “knows” about the file, and it’s not difficult to extract a particular item given it’s name (using a do shell script formulation). It’s also easy to find files given one of these values.

set F to choose file
set PF to quoted form of POSIX path of F
set spotP to do shell script "mdls " & PF
spotP

For movies that QuickTime can open and play, this script by Nigel Garvey (slightly modded here) will do it. Unfortunately, you have to open the movie (which may be a self-starter):

set F to choose file
tell application "QuickTime Player"
	open F
	set {duration:dur, time scale:tscale} to front movie
end tell
set runningTime to dur / tscale
set hr to text 2 thru 3 of ((100 + runningTime div hours) as string)
set min to text 2 thru 3 of ((100 + runningTime mod hours div minutes) as string)
set sec to text 2 thru 3 of ((100 + runningTime mod minutes div 1) as string)
set frm to text 2 thru 3 of ((100 + runningTime mod 1 * 30) as string)
set displayedDuration to hr & ":" & min & ":" & sec & "." & frm

display dialog "Running Time: " & runningTime & " seconds" & return & ¬
	"Displayed duration: " & displayedDuration
tell application "QuickTime Player" to quit

In OSX 10.4 you no longer have to open a movie to get its properties. System Events can get them. This script will do it:

set movie_file to (choose file) as string
tell application "System Events" to set movie_props to properties of contents of movie file movie_file

Therefore to get the running time of a movie you can use this:

set movie_file to (choose file) as string

tell application "System Events"
	set movie_contents to contents of movie file movie_file
	set tscale to time scale of movie_contents
	set dur to duration of movie_contents
end tell

set runningTime to dur / tscale
set hr to text 2 thru 3 of ((100 + runningTime div hours) as string)
set min to text 2 thru 3 of ((100 + runningTime mod hours div minutes) as string)
set sec to text 2 thru 3 of ((100 + runningTime mod minutes div 1) as string)
set frm to text 2 thru 3 of ((100 + runningTime mod 1 * 30) as string)
set displayedDuration to hr & ":" & min & ":" & sec & "." & frm

display dialog "Running Time: " & runningTime & " seconds" & return & "Displayed duration: " & displayedDuration

this line in the code:

tell application "Finder"
		set NF to name of tFolder
		set tFiles to files of entire contents of tFolder
		repeat with F in tFiles

works fine, except when there’s only one file in the folder in question. then it doesn’t run the repeat loop at all.

how do i fix this?

thanks

-b

there are several solutions, one of them is

tell application "Finder"
	set NF to name of tFolder
	set tFiles to files of entire contents of tFolder
	if class of tFiles is not list then set tFiles to {tFiles}
	repeat with F in tFiles

It’s an AppleScript bug. Workaround:

tell application "Finder"
	set NF to name of tFolder
	try
		set tFiles to files of entire contents of tFolder as alias list
	on error -- only one
		set tFiles to files of entire contents of tFolder as alias as list
	end try
end tell