tell finder nested in tell system events does not work
The script gets info about all files in a folder.
The system event part works by itself and the finder part works by itself,
but tell finder doesn’t run nested inside of tell system events !
(some file info is exclusive to either finder or system events. that is why i am using both)
(I am using OsX 10.6)
Thanks, here is the code:
####################################################################sub1
#tell application “Finder”
tell application “System Events”
set theFolder to choose folder
set k to 0
set theText to ""
set delim1 to tab
repeat with fileslist1 in (get files of theFolder)
set k to k + 1
set thisindex1 to (k as text)
####################################################################
#--------------------------------------------the common finder & system event group starts:
#set fileslist11 to (fileslist1 as text)
set displayedname1 to (displayed name of fileslist1)
set nameextension1 to (name extension of fileslist1)
set URL1 to (URL of fileslist1)
set size1 to (size of fileslist1)
set physicalsize1 to (physical size of fileslist1)
set creationdate1 to (creation date of fileslist1)
set modificationdate1 to (modification date of fileslist1)
set class1 to (class of fileslist1)
set kind1 to (kind of fileslist1)
#--------------------------------------------the common finder & system event group ends:
####################################################################
#------------------------the system event only group starts here:
tell application "System Events"
set pfileslist1 to POSIX path of fileslist1
set TextPosixFile1 to (pfileslist1 as text)
#---------
#-------------------------------------------use qpfileslist1 for the shell scripts----------
set qpfileslist1 to quoted form of pfileslist1
set qTextPosixFile1 to quoted form of TextPosixFile1
#-------------
set classicmacfile1 to (pfileslist1 as POSIX file)
set TextClassicmacFile1 to (classicmacfile1 as text)
#-------------
#set classicmacfile1 to quoted form of (pfileslist1 as POSIX file)
#set TextClassicmacFile1 to quoted form of (classicmacfile1 as text)
#=========
# set x1File to quoted form of POSIX path of (fileslist1)
#=========
#set x2File to quoted form of (POSIX path of fileslist1)
#set x3File to POSIX file fileslist1
#set x4File to POSIX file (fileslist1)
#=========
####################################################################
set LastOpen1 to do shell script "date -r $(stat -f %a " & qpfileslist1 & ") +%Y-%m-%dT%H:%M:%S"
#==============
#--- the next output does not have quotes
set LastUsedDate1 to (do shell script "mdls -name kMDItemLastUsedDate -raw " & qpfileslist1)
#--- the next output has openparen-return-quote-stuff-quote-return-endparen
set UsedDates1 to (do shell script "mdls -name kMDItemUsedDates -raw " & qpfileslist1)
copy UsedDates1 as text to singlelineUsedDates1
#==============
#---this is an attempt to remove all linefeeds, it does not work
#set j to 0
#set newtext1 to ""
#repeat with char1 in (get characters of singlelineUsedDates1)
# set j to j + 1
# if (char1 is not equal to linefeed) then
# set newtext1 to newtext1 & char1
# end if
#end repeat
#==============2nd attempt
#set singlelineuseddates2 to my replace_chars(singlelineUsedDates1, "1", "$")
#set singlelineuseddates2 to my replaceText(linefeed, singlelineUsedDates1, "$")
#==============
#display dialog "*" & LastUsedDate1 & "^" & newtext1 & "~" & return
####################################################################first attempt at posix path file, did not work
#set y1file to LastOpenedDate(POSIX file fileslist1)
#set y1file to LastOpenedDate(POSIX file pfileslist1)
#set y1file to LastOpenedDate(fileslist1 as POSIX file)
#set y1file to LastOpenedDate(pfileslist1 as POSIX file)
#====================
####################################################################
end tell
#---the system event only group ends here:
####################################################################the finder only group starts
tell application "Finder"
set index1 to (index of fileslist1)
set fileslist11 to (fileslist1 as text)
set name1 to (name of fileslist1)
set container1 to (container of fileslist1)
set labelindex1 to (label index of fileslist1)
set comment1 to (comment of fileslist1)
#copy (comment of fileslist1) to comment1
set description1 to (description of fileslist1)
end tell
######################### the finder only group ends
set theComboText to (thisindex1) & delim1 & (displayedname1) & delim1 & (URL1) & delim1 & (size1) & delim1 & (creationdate1) & delim1 & (modificationdate1) & delim1 & (class1) & delim1 & (kind1) & delim1
# comment out thefindertext OR thesystemeventtext
set thefindertext to (index1) & delim1 & (name1) & delim1 & (labelindex1) & delim1 & (comment1) & delim1
# comment out thefindertext OR thesystemeventtext
set thesystemeventtext to (pfileslist1) & delim1 & (TextPosixFile1) & delim1 & (classicmacfile1) & delim1 & (TextClassicmacFile1) & delim1
#set xtext1 to (x1File) & delim1 & (x2File) & delim1 & (x3File) & delim1 & (x4File) & delim1
#set ytext1 to (y1file) & delim1
set datetext1 to (LastOpen1) & delim1 & (LastUsedDate1) & delim1 & (UsedDates1) & delim1
#remove either thefindertext OR thesystemeventtext
set theText to theText & thefindertext & theComboText & thesystemeventtext & datetext1 & return
end repeat
end tell
tell application “TextEdit”
launch
activate
make new document with properties {text:theText}
end tell
Model: macbook pro
AppleScript: 2.1.2
Browser: Firefox 25.0
Operating System: Mac OS X (10.6)
What I do if I need to use both Finder and System Events is use System Events first if possible. Then, If I need Finder I end the System Events tell block and use the Finder.
The clou here, is to not have nested tell blocks, tell blocks to applications at least should be as short as possible, and I think rewriting the “nestedness” away will solve your problem.
Here is the working program.
From a chosen folder get the list of contained files with their attributes and format into textedit & excel.
This applescript works. It ran on OsX 10.6 and Excel 2004.
Here is the code:
###############################
–Program: make file directory (of a folder) including attributes.
###############################
–This program asks for a folder selection.
–It then gets a list of the contained files (but not the subfolders)
–and their properties.
–It places this list into textedit and excel (as tab separated fields and CR separated records).
–It first removes any pre-existing CR’s (replacing them with Vertical tabs).
################################################MAIN
tell application “System Events”
set theFolder to choose folder
set k to 0
set theText to ""
set delim1 to tab
#============
#============================================labels
#===index
set indexgroup to ""
set indexgroup to "thisindex0" & delim1
#===dates
set dategroup to ""
#-
set dategroup to dategroup & "AttributeChangeDate1" & delim1
#-
set dategroup to dategroup & "LastOpen2" & delim1
#-
set dategroup to dategroup & "LastUsedDate1" & delim1
set dategroup to dategroup & "UsedDates1" & delim1
#-
set dategroup to dategroup & "FSContentChangeDate1" & delim1
#-
set dategroup to dategroup & "modificationdate3" & delim1
set dategroup to dategroup & "ContentModificationDate1" & delim1
set dategroup to dategroup & "FSCreationDate1" & delim1
#-
set dategroup to dategroup & "creationdate3" & delim1
set dategroup to dategroup & "ContentCreationDate1" & delim1
#===file
set filegroup to ""
set filegroup to filegroup & "DisplayName1" & delim1
set filegroup to filegroup & "displayedname3" & delim1
set filegroup to filegroup & "FSName1" & delim1
#set filegroup to filegroup & "fileslist0" & delim1
#set filegroup to filegroup & "fileslist3" & delim1
#===path
set pathgroup to ""
set pathgroup to pathgroup & "TextPosixFile0" & delim1
set pathgroup to pathgroup & "Path1" & delim1
set pathgroup to pathgroup & "TextClassicmacFile0" & delim1
set pathgroup to pathgroup & "URL3" & delim1
#===kind
set kindgroup to ""
set kindgroup to kindgroup & "nameextension3" & delim1
set kindgroup to kindgroup & "kind1" & delim1
set kindgroup to kindgroup & "kind3" & delim1
set kindgroup to kindgroup & "class3" & delim1
set kindgroup to kindgroup & "ContentType1" & delim1
#===size
set sizegroup to ""
set sizegroup to sizegroup & "size3" & delim1
set sizegroup to sizegroup & "FSSize1" & delim1
set sizegroup to sizegroup & "physicalsize3" & delim1
#===other
set othergroup to ""
set othergroup to othergroup & "FinderComment1" & delim1
set othergroup to othergroup & "FSLabel1" & delim1
#===null
set nullgroup to ""
set nullgroup to nullgroup & "description1" & delim1
set nullgroup to nullgroup & "Creator1" & delim1
set nullgroup to nullgroup & "EncodingApplications1" & delim1
set nullgroup to nullgroup & "WhereFroms1" & delim1
set nullgroup to nullgroup & "SecurityMethod1" & delim1
set nullgroup to nullgroup & "Headline1" & delim1
set nullgroup to nullgroup & "title1" & delim1
set nullgroup to nullgroup & "Instructions1" & delim1
set nullgroup to nullgroup & "Keywords1" & delim1
set nullgroup to nullgroup & "Comment1" & delim1
set nullgroup to nullgroup & "TextContent1" & delim1
set nullgroup to nullgroup & "FSNodeCount1" & delim1
#===
set theText to theText & indexgroup
set theText to theText & dategroup
set theText to theText & filegroup
set theText to theText & pathgroup
set theText to theText & kindgroup
set theText to theText & sizegroup
set theText to theText & othergroup
set theText to theText & nullgroup
###############################
#============================================place end of line record
set theText to theText & "$"
#============================================
#=============
repeat with fileslist0 in (get files of theFolder)
#-
set k to k + 1
set thisindex0 to (k as text)
####################################################################
#--------------------------------------------the common finder & system event group starts:
#---next does not work: can't make classicmac format file .DS_Store into type text
#set fileslist3 to (fileslist0 as text)
#-
set displayedname3 to (displayed name of fileslist0)
set nameextension3 to (name extension of fileslist0)
#-
set URL3 to (URL of fileslist0)
#-
set size3 to (size of fileslist0)
set physicalsize3 to (physical size of fileslist0)
#-
set creationdate3 to (creation date of fileslist0)
set modificationdate3 to (modification date of fileslist0)
#-
set class3 to (class of fileslist0)
set kind3 to (kind of fileslist0)
#--------------------------------------------the common finder & system event group ends:
####################################################################
########################################################### Getting File Names
set pfileslist0 to POSIX path of fileslist0
set TextPosixFile0 to (pfileslist0 as text)
#-------------------------------------------use qpfileslist0 for the shell scripts----------
set qpfileslist0 to quoted form of pfileslist0
set qTextPosixFile0 to quoted form of TextPosixFile0
#-------------
set classicmacfile0 to (pfileslist0 as POSIX file)
set TextClassicmacFile0 to (classicmacfile0 as text)
#-------------
#--- the next does not work: can't make quoted form of classicmac format of file .DS_Store into type reference
#set qclassicmacfile0 to quoted form of classicmacfile0
set qTextClassicmacFile0 to quoted form of TextClassicmacFile0
#-------------
##########################################################Getting File Properties
set LastOpen2 to do shell script "date -r $(stat -f %a " & qpfileslist0 & ") +%Y-%m-%dT%H:%M:%S"
#============== get data
set ContentCreationDate1 to (do shell script "mdls -name kMDItemContentCreationDate -raw " & qpfileslist0)
set ContentModificationDate1 to (do shell script "mdls -name kMDItemContentModificationDate -raw " & qpfileslist0)
set AttributeChangeDate1 to (do shell script "mdls -name kMDItemAttributeChangeDate -raw " & qpfileslist0)
set LastUsedDate1 to (do shell script "mdls -name kMDItemLastUsedDate -raw " & qpfileslist0)
set UsedDates1 to (do shell script "mdls -name kMDItemUsedDates -raw " & qpfileslist0)
set FSCreationDate1 to (do shell script "mdls -name kMDItemFSCreationDate -raw " & qpfileslist0)
set FSContentChangeDate1 to (do shell script "mdls -name kMDItemFSContentChangeDate -raw " & qpfileslist0)
set kind1 to (do shell script "mdls -name kMDItemKind -raw " & qpfileslist0)
set description1 to (do shell script "mdls -name kMDItemDescription -raw " & qpfileslist0)
set ContentType1 to (do shell script "mdls -name kMDItemContentType -raw " & qpfileslist0)
set Creator1 to (do shell script "mdls -name kMDItemCreator -raw " & qpfileslist0)
set EncodingApplications1 to (do shell script "mdls -name kMDItemEncodingApplications -raw " & qpfileslist0)
set WhereFroms1 to (do shell script "mdls -name kMDItemWhereFroms -raw " & qpfileslist0)
set SecurityMethod1 to (do shell script "mdls -name kMDItemSecurityMethod -raw " & qpfileslist0)
set DisplayName1 to (do shell script "mdls -name kMDItemDisplayName -raw " & qpfileslist0)
set Headline1 to (do shell script "mdls -name kMDItemHeadline -raw " & qpfileslist0)
set title1 to (do shell script "mdls -name kMDItemTitle -raw " & qpfileslist0)
set Instructions1 to (do shell script "mdls -name kMDItemInstructions -raw " & qpfileslist0)
set FSName1 to (do shell script "mdls -name kMDItemFSName -raw " & qpfileslist0)
#---sometimes the next does not work
set Path1 to (do shell script "mdls -name kMDItemPath -raw " & qpfileslist0)
set FinderComment1 to (do shell script "mdls -name kMDItemFinderComment -raw " & qpfileslist0)
set FSLabel1 to (do shell script "mdls -name kMDItemFSLabel -raw " & qpfileslist0)
set FSSize1 to (do shell script "mdls -name kMDItemFSSize -raw " & qpfileslist0)
set Keywords1 to (do shell script "mdls -name kMDItemKeywords -raw " & qpfileslist0)
set Comment1 to (do shell script "mdls -name kMDItemComment -raw " & qpfileslist0)
set TextContent1 to (do shell script "mdls -name kMDItemTextContent -raw " & qpfileslist0)
set FSNodeCount1 to (do shell script "mdls -name kMDItemFSNodeCount -raw " & qpfileslist0)
####################################################################
#============================================ assign data
#===index
set indexgroup to ""
set indexgroup to thisindex0 & delim1
#===dates
set dategroup to ""
#-
set dategroup to dategroup & AttributeChangeDate1 & delim1
#-
set dategroup to dategroup & LastOpen2 & delim1
#-
set dategroup to dategroup & LastUsedDate1 & delim1
set dategroup to dategroup & UsedDates1 & delim1
#-
set dategroup to dategroup & FSContentChangeDate1 & delim1
#-
set dategroup to dategroup & modificationdate3 & delim1
set dategroup to dategroup & ContentModificationDate1 & delim1
set dategroup to dategroup & FSCreationDate1 & delim1
#-
set dategroup to dategroup & creationdate3 & delim1
set dategroup to dategroup & ContentCreationDate1 & delim1
#===file
set filegroup to ""
set filegroup to filegroup & DisplayName1 & delim1
set filegroup to filegroup & displayedname3 & delim1
set filegroup to filegroup & FSName1 & delim1
#set filegroup to filegroup & fileslist0 & delim1
#set filegroup to filegroup & fileslist3 & delim1
#===path
set pathgroup to ""
set pathgroup to pathgroup & TextPosixFile0 & delim1
set pathgroup to pathgroup & Path1 & delim1
set pathgroup to pathgroup & TextClassicmacFile0 & delim1
set pathgroup to pathgroup & URL3 & delim1
#===kind
set kindgroup to ""
set kindgroup to kindgroup & nameextension3 & delim1
set kindgroup to kindgroup & kind1 & delim1
set kindgroup to kindgroup & kind3 & delim1
set kindgroup to kindgroup & class3 & delim1
set kindgroup to kindgroup & ContentType1 & delim1
#===size
set sizegroup to ""
set sizegroup to sizegroup & size3 & delim1
set sizegroup to sizegroup & FSSize1 & delim1
set sizegroup to sizegroup & physicalsize3 & delim1
#===other
set othergroup to ""
set othergroup to othergroup & FinderComment1 & delim1
set othergroup to othergroup & FSLabel1 & delim1
#===null
set nullgroup to ""
set nullgroup to nullgroup & description1 & delim1
set nullgroup to nullgroup & Creator1 & delim1
set nullgroup to nullgroup & EncodingApplications1 & delim1
set nullgroup to nullgroup & WhereFroms1 & delim1
set nullgroup to nullgroup & SecurityMethod1 & delim1
set nullgroup to nullgroup & Headline1 & delim1
set nullgroup to nullgroup & title1 & delim1
set nullgroup to nullgroup & Instructions1 & delim1
set nullgroup to nullgroup & Keywords1 & delim1
set nullgroup to nullgroup & Comment1 & delim1
set nullgroup to nullgroup & TextContent1 & delim1
set nullgroup to nullgroup & FSNodeCount1 & delim1
#===
set theText to theText & indexgroup
set theText to theText & dategroup
set theText to theText & filegroup
set theText to theText & pathgroup
set theText to theText & kindgroup
set theText to theText & sizegroup
set theText to theText & othergroup
set theText to theText & nullgroup
###############################
#============================================place end of line record
set theText to theText & "$"
#============================================
end repeat
end tell
#########subroutine that replaces a substring
on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars
############## Replace CR with Vtab {ascii(11 (0B hexad))}. Replace $ with CR {ascii(13 (0d hexad))}.
#—
set cr1 to return
set dollar1 to “$”
set vtab1 to {ASCII character 11}
set theText to replace_chars(theText, cr1, vtab1)
set theText to replace_chars(theText, dollar1, cr1)
#—
####################################################################Place in TextEdit
tell application “TextEdit”
launch
activate
make new document with properties {text:theText}
end tell
####################################################################Place in Excel
set the clipboard to (theText)
tell application “Microsoft Excel”
launch
activate
make new workbook
activate workbook
select sheet 1 of active workbook
select range “A1” of active sheet
paste special on worksheet sheet 1
end tell