Use automator to batch rename images - screenshot

I have a whole folder of screenshots with file names like this:
http_www.mozilla.org.png
http_www.google.com.png
I want to remove every element before the www. and every element after the .org or .com
What should I do in automator on mac to achieve something like this?
Thanks

If you are trying to rename them, try this AppleScript:
tell application "Finder" to set myFiles to files of (choose folder)
repeat with aFile in myFiles
tell application "System Events" to set fileName to aFile's name
set newName to do shell script "echo " & quoted form of fileName & " | sed 's/.*www\\.\\([A-Za-z0-9]*\\).*\\(\\.png$\\)/\\1\\2/'"
tell application "System Events" to set aFile's name to newName
end repeat

Related

Trying to print new folder names to a text edit/numbers file when logged (AppleScript)

I'm trying to add new folders added to a drive onto a textEdit or numbers file so that there's an up to date list of what projects I have active on individual drives.
I'm very new to AppleScript so I don't know what my limitations are, but I basically just need to figure out how to append to the end of the file (it seems like textEdit would be simplest) with the name of the new folder. I currently have:
on adding folder items to theAttachedFolder after receiving theNewItems
-- Get the name of the attached folder
tell application "Finder"
set theName to name of theAttachedFolder
-- Count the new items
set theCount to length of theNewItems
-- Display an alert indicating that the new items were received
activate
display alert "Attention!" message (theCount & " new items were detected in folder. Adding to TextEdit file.")
end repeat
end tell
Any help is much appreciated. Thank you!
The File Read/Write suite in the StandardAdditions scripting dictionary has open for access and write commands that can be used to append to a text file (the AppleScript Language Guide also has a command reference), and AppleScript's text item delimiters or string concatenation can be used to assemble a string from the list of file items.
There are some older topics describing ways to write to a file, but in the following example I've separated the main stuff from the folder action handler so that it can also be called from the run handler for testing in the Script Editor:
property logFile : "/path/to/output/textfile.txt" -- HFS or POSIX
on run -- test
set folderItems to (choose file with multiple selections allowed)
tell application "Finder" to set theFolder to container of first item of folderItems
doStuff(theFolder, folderItems)
end run
on adding folder items to theAttachedFolder after receiving theNewItems
doStuff(theAttachedFolder, theNewItems)
end adding folder items to
to doStuff(theFolder, theItems)
set folderItems to ""
activate
display alert "Attention!" message "" & (count theItems) & " new items were detected in folder." & return & "Adding to log file."
tell application "Finder" to set folderName to name of theFolder
repeat with anItem in theItems
tell application "Finder" to set newName to name of anItem
set folderItems to folderItems & tab & newName & return
end repeat
writeToFile(logFile, "Items added to " & folderName & ":" & return & folderItems & return)
end doStuff
to writeToFile(someFile, someText)
try
set theOpenFile to (open for access someFile with write permission)
write someText to theOpenFile starting at eof -- append
close access theOpenFile
on error -- make sure file is closed
try
close access theOpenFile
end try
end try
end writeToFile

Using AppleScript to programmatically create an AppleScript file in plain text format

I have an AppleScript that is used to programmatically create a test script file in one of these Office 2016 app folders:
~/Library/Application Scripts/com.microsoft.Excel
~/Library/Application Scripts/com.microsoft.Word
~/Library/Application Scripts/com.microsoft.Powerpoint
This is the test.scpt file content which is programmatically generated:
on handlerTest(thisPhrase)
say thisPhrase
end handlerTest
This test.scpt file contains a single handler which speaks the phrase passed to it.
When the script is created in one of these folders, I cannot see the content of the script file in Finder and calling the handler from a Microsoft Office app using the new VBA AppleScriptTask causes the Office app to crash. I think the script is being created as a byte-compiled file because it cannot be viewed in Finder as plain text.
If I then copy the script file generated programmatically by my script creator script to the Documents folder, the plain-text content of the script is viewable in Finder.
Now, if I copy the script file from the Documents folder back to the corresponding com.microsoft folder (without modifying it), I can now see the plain-text content in Finder and calling the handler using the VBA AppleScriptTask function works as expected. I don't understand how the format is apparently changing due to copy/paste actions?
How can I programmatically create the script file in the com.microsoft.xyz folder in plain text format?
Here is my VBA procedure:
Sub TestScript()
AppleScriptTask "test.scpt", "handlerTest", "hello world"
End Sub
Here is my example script creator script which programmatically creates a test.scpt file in the com.microsoft.Powerpoint scripting folder: (kudos to eliteproxy for the original source script)
property theFolders : {"~/Library/'Application Scripts'/com.microsoft.Powerpoint"}
try
tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
set targetFolder to (choose folder)
end try
# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}
do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders
--Write the Script file if it does not exist
if ExistsFile("~/Library/'Application Scripts'/com.microsoft.Powerpoint/test.scpt") is false then
tell application "Finder"
--GET THE WORKING DIRECTORY FOR FILE COPY OF SCRIPT
get folder of (path to me) as Unicode text
set workingDir to POSIX path of result
--Write the new script in the current working directory
set textFile to workingDir & "test.scpt"
--Delete script if it exists
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""
--Create Script Interface file for Microsoft PowerPoint VBA Applications
set fd to open for access textFile with write permission
-- Create test handler which speaks the passed phrase parameter
write "on handlerTest(thisPhrase)" & linefeed to fd as «class utf8» starting at eof
write "say thisPhrase" & linefeed to fd as «class utf8» starting at eof
write "end handlerTest" & linefeed to fd as «class utf8» starting at eof
close access fd
--Copy the script file into the MACOS-Specific 'safe' folder
set fromPath to quoted form of POSIX path of (workingDir) & "test.scpt"
set toPath to quoted form of "~/Library/'Application Scripts'/com.microsoft.Powerpoint"
do shell script "cp -R " & fromPath & space & "~/Library/'Application Scripts'/com.microsoft.Powerpoint" with administrator privileges
end tell
end if
--Delete the temp script file from the working directory
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""
--Provide confirmation
set theAlertTitle to "TEST"
set theAlertMsg to "The script has been successfully installed."
display alert theAlertTitle message theAlertMsg as informational buttons {"OK"} default button "OK" cancel button "OK"
--For use when checking if a file exists
on ExistsFile(filePath)
tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile
I could be wrong in my interpretation of your question, but it appears as if you are looking to create file “Test.scpt” with your handler “handlerTest” as the code, in a folder named “com.microsoft.Excel” (for example). If that is all you are looking to achieve, I believe this solution should work for you...
script theHandler
on handlerTest(thisPhrase)
say thisPhrase
end handlerTest
end script
storeScript()
on storeScript()
set thisScript to store script theHandler in (path to home folder as text) ¬
& "Library:Application Scripts:com.microsoft.Excel:Test.scpt" replacing yes
end storeScript

Applpescript: Reading Text file for List of Files

I have been trying to cobble together a script to take a list of files from a text document and have applescript go through the list line by line and then do something with the file. In this case it is changing the label on the file, but we would use it in other instances to move files, etc.
It works with a test file on my desktop the files get marked with the purple Label, but when trying to run it in the folder I actually need to it fails with this error message:
error "Finder got an error: Can’t set 1 to 5." number -10006 from 1
The text files are the same except for the length of their content.
Could this be a an issues with filenames, and if so how do I make the script more tolerant.
Here is the script:
set textFileContents to POSIX path of (choose file)
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "System Events"
repeat with thisFileName in dataList
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
end tell
Any help would be appreciated, thanks.
1080074 3.tif
1080074 2.tif
1080069_A1.tif
Here is the final code from the solution to this problem and some further work I did.
Thanks to #Mark Setchell & #jackjr300 for all of their patient help.
set justpath to POSIX path of (choose folder with prompt "Select the Folder with Files You Want to Use")
set textFileContents to (choose file with prompt "Select the list of files")
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with FileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (justpath & FileName as POSIX file as alias) to 5
end try
end repeat
end tell
You seem to have a spurious tell application "System Events" in there. It works like this:
set FileName to POSIX path of (choose file)
set FileRecords to paragraphs of (read FileName)
repeat with ThisFileName in FileRecords
say ThisFileName
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
Note that my test file isn't UTF8.
Update
By the way, if all you want do is set the label colour on some files, it may be easier to do that from the Terminal and not worry with Applescript. Say you start the Terminal, and go to your Desktop like this
cd Desktop
you can then change the labels of all files on your Desktop (and in any subdirectories) whose names contain "Freddy" followed by "Frog" (i.e "fileForFreddyFrog.txt", "file from Freddy the Frog.php")
find . -name "*Freddy*Frog*" -exec xattr -wx com.apple.FinderInfo "0000000000000000000700000000000000000000000000000000000000000000" {} \;
You need to specify the folder because the finder has no current folder, except the Desktop if you don't specify a folder.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
set theFolder to "/Users/jack/Desktop/xxx/yyy" as POSIX file as alias -- change it to the path of your folder
tell application "Finder"
repeat with thisFileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of file thisFileName of theFolder to 5
end try
end repeat
end tell
Change the path in the third line of this script
--
If the text file contains the full path of the file, you can use this script.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with thisPath in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (thisPath as POSIX file as alias) to 5
end try
end repeat
end tell

How can I create a display dialog that presents specific file path options for a given PDF file?

I am very new to using Automator and Applescript.
I would like to use Automator and AppleScript to detect PDF files that are downloaded to the "Downloads" folder and opens a display dialog that allows me to select the file path and move the file. So far, what I have (which isn't right) is something like:
set question to display dialog "Save fileName in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3
set answer to button returned of question
if answer is equal to "Figuring Relation" then
tell application "Finder" to move fileName to POSIX file "/Users/mac/Documents/College/Junior/Fall/Art 347 - Figuring Relation"
I want the "Figuring Relation" and "Iconoclasm" buttons to change the file path to a designated file path (I don't want to browse for it), and the "Elsewhere" button to open a Finder window where I can select/browse the path.
If possible, I'm also looking to add the date to the beginning of the file name as "mm-dd_filename".
I am not sure of how to translate the Automator Input to Applescript, or how to include the filename in the display dialog text. Thank you so much for any help.
Here is an example using just applescript. In my example, it assumes you're selecting the file you're wanting to move, but you could easily add something for the script to "Find" all files ending with ".pdf" if you wanted to and then loop through the results.
on run
try
set thisFile to choose file
tell application "Finder" to set currentName to thisFile's name
-- Setting variables for the destinations to be used later
set FiguringRelationPath to (path to documents folder) & "College:Junior:Fall:Art 347 - Figuring Relation:" as string
set IconoclasmPath to (path to documents folder) & "Iconoclasm:" as string
-- Ask the user
set answer to button returned of (display dialog "Save \"" & currentName & "\" in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3)
-- Set the destination variable based on the users response to the dialog
if answer is equal to "Figuring Relation" then
set destination to FiguringRelationPath
else if answer is equal to "Iconoclasm" then
set destination to IconoclasmPath
else
set destination to choose folder with prompt "Please select the destination folder" as string
end if
-- Test that the destination directory exists, if not post the error
try
set destination to destination as alias
on error
error ("Destination path " & destination as string) & " doesn't appear to exist"
end try
-- Rename the file with the date prefix
set tDatePrefix to (do shell script "date '+%m-%d'") & "_" as string
tell application "Finder" to set x's name to tDatePrefix & x's name as string
-- Move the file
tell application "Finder" to move thisFile to destination
on error err
activate
display dialog "Error: " & err buttons {"OK"} default button 1
end try
end run

Applescript open local HTML file with Safari

I'm trying to open a local html using Safari with the following script:
on run
set myPath to (path to me) as text
set myFolderPath to POSIX file (do shell script "dirname " & POSIX path of quoted form of myPath) & ":" as string
set _thispath to myFolderPath & "data:Default.html"
tell application "Safari"
activate
open (_thispath)
end tell
end run
However, the file is trying to open with an apendix of file:/// (an extra slash)
Anyone have any solution to this?
The extra slash is not your problem. First you want to get the "quoted form of the posix path" not the "posix path of the quoted form". That's causing you problems. Plus you aren't converting things to text properly. Anyway, try it this way...
set myPath to path to me
set myFolderPath to POSIX file (do shell script "dirname " & quoted form of POSIX path of myPath)
set _thispath to (myFolderPath as text) & ":data:Default.html"
tell application "Safari"
activate
open (_thispath)
end tell