Folder contents to TextEdit document - applescript

I found this script in https://macscripter.net/viewtopic.php?id=43410:
tell application "Finder"
set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
set theseFiles to files of thisFolder as alias list
end tell
tell application "TextEdit"
activate
repeat with thisFile in theseFiles
set newFilename to my getFilename(thisFile)
set thisDoc to make new document
tell thisDoc
make new attachment with properties {file name:thisFile}
set pathtofile to ((thisFolder as string) & newFilename & ".rtfd") as Unicode text
save in file pathtofile
end tell
close front document saving no
end repeat
end tell
on getFilename(thisFile)
set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set theFilename to last text item of (thisFile as text)
set AppleScript's text item delimiters to "."
set newFilename to first text item of theFilename
set AppleScript's text item delimiters to atid
return newFilename
end getFilename
This script do one TextEdit file per one file of source folder.
How can I modify this script to creates one TextEdit file of all files of source folder? The result file will contain all folder files.
I do not need a list of file names into TextEdit document. I need all FILES into TextEdit document with attachments (RTFD format).

I'll confess that I'm not precisely clear what an 'attachment' is in TextEdit lingo, or what it might be used for, but if you just want one file with all of these attachments added to it it, that's easy enough:
tell application "Finder"
set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
set theFileName to name of thisFolder
set theseFiles to files of thisFolder as alias list
end tell
tell application "TextEdit"
activate
set thisDoc to make new document
set pathtofile to ((thisFolder as string) & theFileName & ".rtfd") as Unicode text
tell thisDoc
repeat with thisFile in theseFiles
make new attachment with properties {file name:thisFile}
end repeat
save in file pathtofile
end tell
close front document saving no
end tell
EDIT
Per the comments, here's a version that applies this routine to subfolders. I've cleaned up the code a bit (I like using System Events better than the Finder), but it's the same process.
set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
tell application "System Events"
set subFolders to folders of thisFolder
repeat with aFolder in subFolders
set folderName to name of aFolder
set filePath to (POSIX path of thisFolder) & "/" & folderName & ".rtfd"
set fileList to (POSIX path of every file of aFolder whose visible is true)
tell application "TextEdit"
activate
set thisDoc to make new document
tell thisDoc
repeat with aFile in fileList
make new attachment with properties {file name:(POSIX file aFile) as alias}
end repeat
save in POSIX file filePath
end tell
close front document saving no
end tell
end repeat
end tell
return

I know you can do something like what you may be looking for in command line.
ls path_to_folder > path_to_export_file.txt
Example:
ls ~/Desktop/ > ~/Desktop/export2.txt
I am pretty sure you would be able to integrate that into AppleScript to do whatever else you need.

Related

Checking if an item exists as a folder using POSIX notation in AppleScript

I have a string which represents a File/Folder in a Folder in POSIX notation:
/Users/surfacedetail/Desktop/Temp/Test1
I want to check if the the Test1 exists AND is a Folder. The following tells is that it exists:
if exists my POSIX file myFileOrFolder then
beep
end if
This works ok, but how to tell if it is a Folder or a File?
The problem is that this is part of a massive script, this is a better test, it uses system events, but it doesn't work. I think its something to do with how I am building the path string.
tell application "Finder"
set myScriptFilePath to (path to me)
set mySourceFolder to folder of myScriptFilePath
set myFileAliasList to the entire contents of mySourceFolder
set myPOSIXSourceFolder to URL of mySourceFolder
set myPOSIXSourceFolder to characters 8 thru -1 of myPOSIXSourceFolder as string
repeat with myFile in myFileAliasList
set myFileName to name of myFile
log "myFileName: " & myFileName
if (myFileName ends with ".txt") then
set AppleScript's text item delimiters to "."
set myFolderName to first text item of myFileName
set myFolderPath to myPOSIXSourceFolder & myFolderName as string
log "myFolderPath: " & myFolderPath
tell application "System Events"
if not (exists folder myFolderPath) then
beep
display dialog "Could not find Folder for File: " & myFileName
return {}
end if
end tell
end if
end repeat
end tell
You can do it with System Events without any type cast
set thePath to "/Users/surfacedetail/Desktop/Temp/Test1"
tell application "System Events"
if exists folder thePath then
beep
end if
end tell
Fixed it! It was because I wasn't restoring the text delimiter, it needed this:
set mySaveASDelimiters to AppleScript's text item delimiters
change delimiters
set AppleScript's text item delimiters to mySaveASDelimiters

Combining two (applescript) scripts into one

I'm trying to simplify my workflow by combining the following two applescripts into one, they work fine separately but it'd be more efficient to combine them.
In short, script A trims a file name into the last 3 characters and Script B adds the folder name (where the files reside) to the file name.
This might be a very simple fix but I'm script-writing challenged so any help is welcomed.
SCRIPT A:
on open whichFile
repeat with aFile in whichFile
tell application "Finder"
set filename to name of aFile
set name of aFile to ((characters -1 thru -7 of filename) as string)
--set name of whichFile to ((characters 1 thru -4 of filename) as string) --trim last 3
end tell
end repeat
end open
SCRIPT B
on open theDroppedItems
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set theCurrentDroppedItem to theCurrentDroppedItem as string
tell application "System Events"
set folderPath to theCurrentDroppedItem as string
--display dialog (folderPath)
set AppleScript's text item delimiters to ":"
set newFileName to (text item -4 of folderPath as string) & "-" & (text item -2 of folderPath as string) & "-" & (text item -1 of folderPath as string)
--display dialog (newFileName)
--rename file
set fileAlias to (theCurrentDroppedItem) as alias
set the name of fileAlias to newFileName
end tell
end repeat
end open
You can start by opening a new Script Editor document for the combined script. The open handler is passed a list of file items, so you can add a new handler declaration with an empty repeat statement that steps through the dropped items. From there, just identify the statements that perform the various operations (trim name, get folder name, etc), and copy them into the new open handler's repeat statement, editing as needed to use consistent variable names.
Once the new script is running, then you can look at optimizing it by combining and/or rearranging statements that may be doing the same thing in the different scripts. It can also be helpful to organize functions into their own handlers, such as the getNamePieces handler below. I also like to add a run handler with a choose file dialog so that you can test without having to drag items onto a droplet.
Note that the file name includes any extension, so you should break it apart in order to work with just the name part. There is also a bit of unnecessary thrashing about in the script that gets the folder name, so after cleaning it up your script could look something like:
on run
open (choose file with multiple selections allowed)
end run
on open droppedItems
repeat with anItem in droppedItems
set {folderPath, theName, extension} to getNamePieces from anItem
set trimmedName to text -1 thru -3 of theName -- work with just the name part
tell application "System Events"
set folderName to name of disk item folderPath
set newName to folderName & "-" & trimmedName & extension -- assemble the pieces
log newName -- for testing
# set name of anItem to newName -- uncomment to go live
end tell
end repeat
end open
to getNamePieces from someItem -- return the containing folder path, the name, and the extension
tell application "System Events" to tell disk item (someItem as text)
set theContainer to path of the container
set {theName, extension} to {name, name extension}
end tell
if extension is not "" then
set theName to text 1 thru -((count extension) + 2) of theName -- just the name part
set extension to "." & extension
end if
return {theContainer, theName, extension}
end getNamePieces

Reading file in AppleScript

I'm trying to read an html file into a variable in AppleScript, I have the following code.
tell application "Finder"
set theItems to every file of folder folderName
repeat with theFile in theItems
open for access theFile
set fileContents to (read theFile)
end repeat
end tell
Now I get an error like:
Finder got an error: Can’t make document file "index.html" of folder
[...] of startup disk into type «class fsrf».
What am I doing wrong? I followed this example. Are HTML files not recognized as text?
You have to convert the Finder file objects to aliases or text.
read can be used without separate open or close commands. It reads files as MacRoman without as «class utf8» though. (as Unicode text is UTF-16.)
tell application "Finder" to files of folder "HD:Users:lauri:Sites" as alias list
repeat with f in result
read f as «class utf8»
end repeat
Try:
tell application "Finder" to set theItems to every file of folder folderName
repeat with theFile in theItems
set aFile to POSIX path of (theFile as text)
set fileContents to do shell script "cat " & quoted form of aFile
end repeat
Starting from your original code, this should do it:
set folderPath to choose folder
set someData to ""
tell application "Finder"
set theItems to every file of folder folderPath as list
repeat with theFile in theItems
set theFilePath to theFile as text
if characters -5 thru -1 of theFilePath as string is ".html" then
set theFileHandle to (open for access file theFilePath)
set fileContents to (read theFileHandle)
-- for testing, call some function
set someData to someData & return & processHtml(fileContents) of me
close access theFileHandle
end if
end repeat
-- do something with someData here
return someData
end tell
on processHtml(theData)
-- do something with theData here
return theData
end processHtml
As Lauri wrote, you can add "as «class utf8»" to read the file as UTF8. You could also use "as Unicode text" for UTF16. Personally, I like this, because it is vanilla AppleScript and doesn't need shell scripting.
Using open for access is really doing it the hard way.
If you want to read an HTML file with AppleScript, then the best way to do that is to use AppleScript to tell an HTML editor to read the HTML file for you. That is the fundamental way that AppleScript works. That’s why “tell” is the most important command. That’s why you can accomplish your goal of reading an HTML file into a variable in just 3 lines:
tell application "BBEdit"
open (choose file)
set theHTMLSource to the text of document 1
close document 1
end tell
The following script expands on the above to read an arbitrary number of HTML files from a chosen folder. It works with BBEdit 9, and should also work with BBEdit’s free version, which is called “TextWrangler” and is available in Mac App Store. Or you can fairly easily adapt this script for use with HyperEdit or TextEdit or whatever AppleScript-aware HTML/text editor you prefer to use.
tell application "Finder"
set theFolder to (choose folder)
set theFiles to every file of folder theFolder
set theHTMLSourceList to {}
repeat with theFile in theFiles
if the kind of theFile is equal to "HTML document" then
set theName to the name of theFile
tell application "BBEdit"
open file (theFile as text)
set theSource to the text of document 1
copy {theName, theSource} to the end of theHTMLSourceList
close document 1
end tell
end if
end repeat
end tell
When the above script is finished, the variable “theHTMLSourceList” is populated with the names and source code of the entire folder of HTML documents, like so:
{{name of file 1, source of file 1}, {name of file 2, source of file 2}, {name of file 3, source of file 3}}
… and so on up to an arbitrary number of files. But of course you can have the script return the HTML source to you in whatever way you like. The key point is that an AppleScript-aware HTML editor can both read HTML and set AppleScript variables, so you don’t have to write (and debug and maintain) your own HTML reader in tiny AppleScript.

AppleScript: Create new folder from file name and move file into that folder

Basically, I'm trying to create a folder action in Automator so whenever a file is added to a specific folder, it will create a subfolder that matches the filename (without the extension), then move the file into that subfolder.
So far, I have successfully used a post from this site (Create new folder named with a specified file name in Automator) to create a script that will create the new folder. However, I have been unable to modify the script to move the original file into the new folder.
Any help would be appreciated. Here is the full script I am working with for reference:
on run {input, parameters} -- make new folders from base file names
set output to {}
repeat with anItem in the input -- step through each item in the input
set anItem to anItem as text
tell application "System Events" to tell disk item anItem
set theContainer to path of container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
tell application "Finder"
make new folder at folder theContainer with properties {name:theName}
set end of output to result as alias
end tell
end repeat
return input -- or output
end run
Thanks in advance!
Add this folder action to your target folder:
on adding folder items to theFolder after receiving theFiles
repeat with aFile in theFiles
tell application "System Events" to set {Nm, Ex, pPath} to aFile's {name, name extension, POSIX path of container}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
set thePath to (pPath & "/" & BN & "/" as text)
do shell script "mkdir -p " & quoted form of thePath
delay 0.5
tell application "Finder" to move aFile to POSIX file thePath
end repeat
end adding folder items to

Applescript create and rename file from other files

In order to import .MOV files (h.264) to Final Cut Pro I need a correspoding .THM file with the same filename as the .MOV. Is it possible to do this with an AppleScript or Automator? Here is what I want to do:
Create a copy of a "TEMPLATE.THM" file that already exists on my HD
Rename the "TEMPLATE.THM" file using the .MOV filename
Do this to a folder of .MOV files to create a .THM file for every .MOV file both with the same filename.
G'day
This might not be the quickest way — but I see you're still waiting for an answer — so here's something to get you started. Select all your MOV files in the finder and run this in script editor.
set theTemplate to "Macintosh HD:Users:[user name]:[folder:location]:TEMPLATE.THM"
tell application "Finder"
set theFiles to selection
repeat with thisFile in theFiles
set thisName to name of thisFile
set theFolder to container of thisFile
set newFile to duplicate theTemplate to theFolder
set text item delimiters of AppleScript to "."
set thisName to text item 1 of thisName
set text item delimiters of AppleScript to ""
set newName to (thisName & ".THM")
set name of newFile to newName
end repeat
end tell
The easiest way to get the path to the template is to select it in the finder and run this :
tell application "Finder"
set theFile to selection as string
end tell
That will put the path in your results window — just copy it into the first line of the script above.
Hope that helps
m.

Resources