Applescript (or automator action) to convert HTML to Plain Text - applescript

I had an Applescript or Automator action that let me select files and then it would convert the ones that were HTML to plain text. Somehow I may have lost that script.
I think the script was the one below, but when I use it and select some files, it converts some of them to .rtf and then they disappear. Bizarre.
Any help would be greatly appreciated.
set ASTID to AppleScript's text item delimiters
choose file with multiple selections allowed without invisibles
repeat with thisFile in result
try
set thisFile to POSIX path of thisFile
do shell script "/usr/bin/textutil -convert rtf " & quoted form of result
set AppleScript's text item delimiters to {"."}
set thisFile to text items 1 through -2 of thisFile
set AppleScript's text item delimiters to {""}
set thisFile to thisFile as text
do shell script "/System/Library/Printers/Libraries/convert" & ¬
" -f " & quoted form of (result & ".rtf") & ¬
" -o " & quoted form of (result & ".pdf") & ¬
"; /bin/rm -f " & quoted form of (result & ".rtf")
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
end repeat
set AppleScript's text item delimiters to ASTID

Related

How to save your desktop elements (icons, files,folder ) always on the same place with applescript

With the script we are going to create another script where will be store position of all the elements of the desktop, the created script will be compile and usable to put back in place all the elements previously protected.
/adesktopsave/deskico.txt it is the temporary file which will be of use to the compilation.
/adesktopsave/savedicoposition.scpt It is the script of saving that is compiled to be used with applescrit
All the names used here exist that just for the example. These names have no particular property.
It is just necessary to plan to create a folder before using this script. Here it is:
/adesktopsave
Something else, end of line (\n) after " try
"
also " end try
"
and & "}
")
Are very important to respect so that the text is usable.
tell application "Finder" to set theList to {name, desktop position} of items of desktop
try
do shell script "rm -f /adesktopsave/deskico.txt"
do shell script "echo tell application " & quoted form of (quote & "Finder" & quote) & return & " >>/adesktopsave/deskico.txt"
end try
set n to (count (first item of theList))
repeat with i from 1 to n
set inp to do shell script "echo " & quoted form of (item i of first item of theList)
set xy to (item i of second item of theList)
set AppleScript's text item delimiters to ","
set xyz to do shell script "echo " & xy
set wxyz to ("{" & xyz & "}
")
set ligne to "try
" & "set desktop position of item " & quoted form of (quote & inp & quote) & " of desktop to " & quoted form of (wxyz) & "end try
"
set ligne to do shell script "echo " & ligne & " >>/adesktopsave/deskico.txt"
end repeat
do shell script "echo " & "end tell" & return & " >>/adesktopsave/deskico.txt"
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt" & " /adesktopsave/deskico.txt"
end if
return n
We can lighten the script to its simplest expression. At the risk of having errors can be.
set ligne to ""
do shell script "mkdir -p /adesktopsave"
tell application "Finder" to set {names, positions} to {name, desktop position} of items of the desktop
set ligne to "tell application \"Finder\"
"
set n to (count names)
set AppleScript's text item delimiters to ","
repeat with i from 1 to n
set ligne to ligne & ("try
" & "set desktop position of item " & (quote & item i of names & quote) & " to {" & item i of positions & "}
end try
")
end repeat
set ligne to ligne & ("end tell" & return)
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt -e " & quoted form of ligne
end if
set AppleScript's text item delimiters to ""
tell application "Finder" to open POSIX file "/adesktopsave/savedicoposition.scpt"
return n

Sub-Routine POSIX path issue.

Working on a script that downloads files and places them in a folder for for automated installation. This is the first time I've created an "on" sub-routine and I'm having issues with resolving the path to check if the file exists. The process uses a Terminal window to monitor the download status.
set theContentPath to POSIX path of (choose folder)
set toInstall to (theContentPath & "ToInstall/")
set inStalled to (theContentPath & "Installed/")
set theTemp to (theContentPath & "theTemp/")
do shell script "mkdir -p " & quoted form of toInstall
do shell script "mkdir -p " & quoted form of inStalled
do shell script "mkdir -p " & quoted form of theTemp
set theList to {"http://url1.pkg",
"http://url2.pkg",
"http://url3.pkg",
"http://url4.pkg",
"http://url5.pkg",
"http://url6.pkg"}
repeat with x from 1 to (count theList)
--display dialog item x of theList
set thisOne to item x of theList
getFileIfNeeded(thisOne, toInstall, theTemp)
end repeat
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theItems to text items of theFileToGet
set theFile to item 5 of theItems
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "Finder"
if not (exists file theFileCheck) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded
Tried adding the following set theFileCheck2 to (POSIX file theFileCheck) as string but the results are not what I expected as the file still get's downloaded.
Here's the routine with my attempt to get the path right.
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theItems to text items of theFileToGet
set theFile to item 5 of theItems
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "Finder"
set theFileCheck2 to (POSIX file theFileCheck) as string
if not (exists file theFileCheck2) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded
This should fix it.
It targets "System Events" instead of "Finder", a lot of the functionality previously in the Finders dictionary has been shifted to System Events over the last several years.
Also i simplified the part that extracts theFile, you can use the 'last' keyword in this case instead of a hard coded number.
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theFile to the last text item of theFileToGet
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "System Events"
if not (exists file theFileCheck) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded

Applescript to add grandparent folder+parent folder prefix to filename

I have multiple folders with sub folders that have files in them that need to be labeled with their parent folder+grandparent folder name.
i.e. Folder 1>Folder 2>File.jpg needs to be renamed to Folder_1_Folder_2_File.jpg
I was able to find a script that somewhat does it, and have been trying to reverse engineer it, but am not having any luck. The script below presents two challenges, 1) It includes the entire path from the root directory, and two, it deletes the name of the file, therefore only allowing one file to be renamed before it errors out. I know that the problem is that the script is renaming the entire file, I just don't know how to proceed.
tell application "Finder"
set a to every folder of (choose folder)
repeat with aa in a
set Base_Name to my MakeBase(aa as string)
set all_files to (every file in aa)
repeat with ff in all_files
set ff's name to (Base_Name & "." & (ff's name extension))
end repeat
end repeat
end tell
to MakeBase(txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set new_Name_Raw to every text item of txt
set AppleScript's text item delimiters to "_"
set final_Name to every text item of new_Name_Raw as text
set AppleScript's text item delimiters to astid
return final_Name
end MakeBase
Thank you!
tell application "Finder"
repeat with theItem in (the selection as list)
set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
end repeat
end tell
If you want to learn how AppleScript can work with an app, look through the app's dictionary of AppleScript commands (AppleScript Editor > File > Open Dictionary...).
Edit 1
Here's a version in which you select the "grandparent folder" containing folders containing the items to rename:
tell application "Finder"
set itemsToRename to {}
set selectedFolders to (the selection as list)
repeat with selectedFolder in selectedFolders
set childFolders to every item of selectedFolder
repeat with childFolder in childFolders
set grandchildItems to every item of childFolder
set itemsToRename to itemsToRename & grandchildItems
end repeat
end repeat
repeat with theItem in itemsToRename
set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
end repeat
end tell
Try:
set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat

How to get the Color profile of an Image using Applescript?

)
I want to write an apple-script which collect the color profile of an image.. can anyone please help me out how to do this? I've no idea!!
Thanks in advance.
I like using ExifTool by Phil Harvey to extract metadata. Here is a service I wrote to access the metadata quickly.
on run {input, parameters}
-- creates a metadata folder in the Documents folder to store results
set thePath to POSIX path of (path to documents folder) & "metadata" & "/"
do shell script "mkdir -p " & quoted form of POSIX path of thePath
set {inputFiles, outputFiles} to {{}, {}}
repeat with anItem in input
set end of inputFiles to quoted form of POSIX path of (anItem as text)
tell application "Finder" to set {name:fileName, name extension:nameExtension} to anItem
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set end of outputFiles to quoted form of (thePath & baseName & ".txt")
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set {inputFiles, outputFiles} to {(inputFiles as text), (outputFiles as text)}
set text item delimiters to TID
do shell script "exiftool -a " & inputFiles & " -w " & quoted form of (thePath & "%f.txt" as text) & "; open " & outputFiles
end run

Strip file path info to • and / text item delimiter

The script below basically you choose a folder with PDFs, get file count of PDFs on chosen folders, write the results in text file, open the text file in Excel. The scripts works fine but I get entire the file path.
Results are:
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: 65
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: RESENDS 0
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: RESENDS 6
I want to strip everything before the bullet • then for every / a column. Something like this:
CUSTO_4 BODY 65
CUSTO_4 BODY RESENDS 0
CUSTO_4 COVERS 23
CUSTO_4 COVERS RESENDS 6
I trying to grasp the concept of text item delimiters and using the offset command but I don't know how to implement that into the script.
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisible
set results to ""
repeat with i from 1 to (count target_folder)
set thisFolder to (POSIX path of item i of target_folder)
--Find & count all PDFs in folders selected that DON'T starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
set results to (results & "" & thisFolder & ":" & fileCount & return)
--Find & count all PDFs in folders selected that starts with letter R
set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
set results to (results & "" & thisFolder & ":" & tab & tab & "RESENDS" & fileCount & return)
end repeat
--write results to a txt file
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
set eof of theFile to 0
write results to file theFilePath
close access theFile
on error
close access theFile
end try
--Will open the the PDF File Count.txt in Excel
tell application "Microsoft Excel"
activate
open text file filename "PDF File Count.txt"
end tell
AppleScript's text item delimiters are used to determine how text is broken apart and/or reassembled. When you get the text items of a string, the string is broken apart at each delimiter, and the result is a list of the pieces. Going the other way, if you coerce a list of text items to a string, the pieces are reassembled with the delimiter used in between each piece.
For your example, you could use something like the following (I added a little formatting to get your result):
set theList to {¬
"/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: 65", ¬
"/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: RESENDS 0", ¬
"/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23", ¬
"/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: RESENDS 6"}
set finalResult to {} -- this will be the final result
set tempTID to AppleScript's text item delimiters -- stash the original delimiters
repeat with anItem in theList
set AppleScript's text item delimiters to "•"
set pieces to text items of anItem -- break apart at bullets
log result
set theFile to (rest of pieces) as text -- drop the first piece and reassemble
set AppleScript's text item delimiters to "/"
set pieces to text items of theFile -- now break apart at slashes
log result
set lastPiece to last item of pieces -- trim the last piece a bit
set theCount to 0
repeat while first character of lastPiece is in {space, ":"}
set lastPiece to text 2 thru -1 of lastPiece -- trim it
set theCount to theCount + 1 -- count up trimmed characters
end repeat
if theCount > 4 then set lastPiece to tab & tab & lastPiece -- add a little formatting...
set last item of pieces to lastPiece -- put the trimmed piece back
set text item delimiters to tab & tab
set pieces to pieces as text -- put the pieces back together with tabs
log result
set end of finalResult to pieces -- store the reassembled text for later
end repeat
set AppleScript's text item delimiters to tempTID -- restore the original delimiters
choose from list finalResult with empty selection allowed -- show the results
You don't always have to use text item delimiters to manipulate text:
set xxx to "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: 65
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: RESENDS 0
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: RESENDS 6"
set yyy to do shell script "echo " & quoted form of xxx & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
and the other approach:
set xxx to "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: 65
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/: RESENDS 0
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23
/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: RESENDS 6"
-- break apart and capture original delimiters
set {Astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•"}
set yyy to text items 2 thru -1 of xxx
--put together
set AppleScript's text item delimiters to {""}
set yyy to yyy as string
-- break apart
set AppleScript's text item delimiters to ":"
set yyy to text items of yyy
--put together
set AppleScript's text item delimiters to {""}
set yyy to yyy as string
-- break apart
set AppleScript's text item delimiters to "/"
set yyy to text items of yyy
--put together
set AppleScript's text item delimiters to tab
set yyy to yyy as string
-- reset original delimiters
set AppleScript's text item delimiters to Astid
return yyy

Resources