Applescript image filenames to spreadsheet

I'm a long time mac user but just recently discovered Applescript, TODAY! Looks amazing so far but I need some help to accomplish a simple (I think) task.


I have a spreadsheet in numbers full of products (sku, price, blah blah blah). I have a folder full of images for all these products. The images are titled beginning with the sku followed by extra info like a _1 or _detail or -closeup etc.


I want to basically create a script that will go through this spreadsheet, look at the SKU in each row, then find all the related image files within a specific folder (folder has subdirectories) and insert all matching filenames into a column next to the SKU, separated by a semicolon.


For example,


skugallery_images
abcd1234abcd1234.jpg;abcd1234_1.jpg;abcd_detail.png;abcd_closed.gif
defg5678defg5678.png;defg5678_2.jpg;defg5678-lifestyle.jpg
hijk91011hijk91011.jpg;hijk91011-2.jpg;hijk91011-5.jpg


Any and all help would be much appreciated.

MacBook Pro, Mac OS X (10.5.8)

Posted on Sep 13, 2011 9:30 PM

Reply
3 replies

Sep 14, 2011 1:25 AM in response to mrwinston

I tried this here with a sample folder.... seems to work...


so you select the lines you want to process with the script (columns sku and gallery_images) and the script overwrites the cells in "gallery_images" column with all found filenames....


--14.09.2011 hubionmac.com

-- parts of numbers selection code by http://hints.macworld.com/article.php?story=20090109055630154

-- requested @https://discussions.apple.com/thread/3325583?tstart=0

-- this script takes the value of a selection in numbers first and a asks for a folder

-- then the values of the first column of the selection are taken and all filenames of start start with that folder and are found in the defined folder will be

-- writen to the last column of the selection ----> you will have to select at least 2 columns


set thefolder to quoted form of POSIX path of ((choose folder) as alias)


tell application "Numbers"

activate

tell document 1

-- DETERMINE THE CURRENT SHEET

set current_sheet_index to 0

repeat with i from 1 to the count of sheets

tell sheet i

set x to the count of (tables whose selection range is not missing value)

end tell

if x is not 0 then

set the current_sheet_index to i

exit repeat

end if

end repeat

if the current_sheet_index is 0 then error "No sheet has a selected table."

-- GET THE VALUES OF THE SELECTED CELLS

tell sheetcurrent_sheet_index

set the current_table to the first table whose selection range is not missing value

tell the current_table

set range_column_count to count of every column of selection range

set this_rows_first_cell_index to 1

set this_rows_last_cell_index to range_column_count

-- THIS CHANGES THE VALUE OF

repeat with i from 2 to count of every cell of selection range

set thename to value of cellthis_rows_first_cell_index of selection range

set foundfilenames to my find_filenames(thefolder, thename)

set value of cellthis_rows_last_cell_index of selection range to foundfilenames

set this_rows_first_cell_index to this_rows_first_cell_index + range_column_count

set this_rows_last_cell_index to this_rows_last_cell_index + range_column_count

if this_rows_last_cell_index > (count of every cell of selection range) then exit repeat

end repeat

end tell

end tell

end tell

end tell



on find_filenames(thefolder, filename_start)

-- THIS SEARCHES FILES STARTING WITH A SPECIFIC STRING IN THEIR NAMES AND OUTPUTS JUST THEIR FILENAMES

-- IS SEARCHES IN THEFOLDER AND ALL SUB-DIRECTORIES

set theoutput to do shell script "find " & thefolder & " -iname \"" & filename_start & "*\" -exec basename {} \\;"

if theoutput ≠ "" then

set theoutput to every paragraph of theoutput

set AppleScript's text item delimiters to "; "

set theoutput to theoutput as text

set AppleScript's text item delimiters to ""

end if

return theoutput

end find_filenames

Sep 14, 2011 1:34 PM in response to mrwinston

I tweaked the find function a bit, so that it returns the relative paths instead of just the filenames


on find_filenames(thefolder, filename_start)

-- THIS SEARCHES FILES STARTING WITH A SPECIFIC STRING IN THEIR NAMES AND OUTPUTS JUST THEIR FILENAMES

-- IS SEARCHES IN THEFOLDER AND ALL SUB-DIRECTORIES

set theoutput to do shell script "cd " & thefolder & ";find " & "." & " -iname \"" & filename_start & "*\""

if theoutput ≠ "" then

set theoutput to every paragraph of theoutput


--OPTIONAL REMOVE THE FIRST CHARACTER OF EACH PATH

set tmp to {}

repeat with t in theoutput

set tmp to tmp & {(characters 2 through -1 of t) as text}

end repeat

set theoutput to tmp



set AppleScript's text item delimiters to "; "

set theoutput to theoutput as text

set AppleScript's text item delimiters to ""

end if

return theoutput

end find_filenames

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript image filenames to spreadsheet

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