I’m kind of rookie when it comes to applescript and I was wondering if someone can help me with some little tweak I need to do.
What I’m trying to achieve is create an Applescript that will let me
1- Ask to select a source folder and a new destination to copy it
2- Ask to create an email to a group of people (found in Adress book)
3- Once the email is setup, start the copy
4- Once the copy is done, find all .mhl files that are in the destination folder and run this recursive cmd to all of those
mhl files in the terminal ''mhl verify -f “name of the .mhl”"
5- Once all the mho verification pass, send the email set on step 2.
Here’s the script I’ve created so far, there’s a couple of little things missing that I hope you can help me with
1- How to pass the name of the source folder so it became the default Subject of my newMessage
(right now it’s ‘’‘’)?
2- Once the copy’s complete, hows find all .mhl files in the destination folder and run the terminale cmd to each one of
them?
3- Is it possible to send an email to predefine people and stop everything if the copy or mho verification fail?
Thanks a lot, here’s the applescript I’ve been working on.
tell application "Finder"
set sourceFile to choose folder with prompt "Choose a folder to copy:"
set targetFolder to choose folder with prompt "And where do you want it to go?"
end tell
property defaultGroups : {"Family", "Friends"}
property defaultLabels : {}
-- Prompt for the group to use
tell application "Contacts" to set everyGroup to name of every group
if (count of everyGroup) is greater than 0 then
set theGroups to choose from list everyGroup with prompt ¬
"Send this message to which group?" default items defaultGroups with multiple selections allowed
end if
-- Prompt for message subject
set theResult to display dialog "What would you like the subject of the message to be?" default answer "I'm sending this via AppleScript!"
set theSubject to text returned of theResult
-- Prompt for whether an attachment is desired. If so, prompt for the location of the file.
set theResult to display dialog "Would you like to attach some files to this message?" buttons {"Yes", "No"} default button 1
set wantsAttachment to button returned of theResult
if wantsAttachment is equal to "Yes" then
set theAttachment to choose file
end if
-- Prompt for message body
set theResult to display dialog "What would you like to say in the body of the message?" default answer ""
set theBody to text returned of theResult
-- Go through each account and constuct a list of possible addresses
-- to use as a return address for this message.
tell application "Mail"
set listOfSenders to {}
set everyAccount to every account
repeat with eachAccount in everyAccount
set everyEmailAddress to email addresses of eachAccount
if (everyEmailAddress is not equal to missing value) then
repeat with eachEmailAddress in everyEmailAddress
set listOfSenders to listOfSenders & {(full name of eachAccount & " <") as string}
end repeat
end if
end repeat
end tell
-- Prompt the user to select which account to send this message from.
set theResult to choose from list listOfSenders with prompt "Which account would you like to send this message from?" without multiple selections allowed
if theResult is not equal to false then
set theSender to item 1 of theResult
tell application "Mail"
-- Properties can be specified in a record when creating the message or
-- afterwards by setting individual property values.
set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
tell newMessage
-- Default is false. Determines whether the compose window will
-- show on the screen or whether it will happen in the background./3693.
set visible to true
set sender to theSender
set theEmails to {}
tell application "Contacts"
repeat with aGroup in theGroups
repeat with aPerson in people of group aGroup
repeat with anEmail in emails of aPerson
if (label of anEmail is in defaultLabels) or defaultLabels is {} then
if theEmails does not contain the value of anEmail then
copy (value of anEmail) to end of theEmails
end if
end if
end repeat
end repeat
end repeat
end tell
repeat with anEmail in theEmails
make new to recipient at end of to recipients with properties {address:anEmail}
end repeat
tell content
if (wantsAttachment is equal to "Yes") then
-- Position must be specified for attachments
make new attachment with properties {file name:theAttachment} at after the last paragraph
end if
end tell
end tell
tell application "Finder"
try
duplicate sourceFile to targetFolder
on error errMsg number errNum
display dialog "Error: " & errNum & return & errMsg
end try
update targetFolder
end tell
delay 4
-- Bring the new compose window to the foreground, in all its glory
activate
send newMessage
end tell
end if
Ok, so I did work on it and I’m almost there. The only thing missing is how can I look in my target folder to get only the .mhl file so I can run the terminal command on them. I’ve found a way to check my .mhl file using applescript (shown in the script bellow) But that script is only working in a ‘‘droplet’’ mode. I have to manually get my mho files once the copy is done and drag them on the Applescript app to make it work. Is there a way to integrate this in my previous script so once the copy’s done, the script search for all .mhl in the target folder and feed them as an input?
Thanks again
here’s the ‘‘droplet’’ script to verify mho
on open filelist
repeat with i in filelist
set filecount to 1
tell application “Terminal”
activate
do script "mhl verify -f " & POSIX path of i & “”
end tell
end repeat
end open
I can’t get your ‘mhl verify’ to work, so had to do it with rtf on the desktop (target folder). Something like this:
tell application "Finder"
set file_list to (every file of desktop whose name extension is "rtf") as alias list
end tell
repeat with this_file in file_list
set pp to quoted form of (POSIX path of this_file)
set cmd to "osascript -e '" & "say \"hello\"'"
do shell script cmd
end repeat
Instead of using Terminal it uses ‘do shell script’, running the osascript shell command. The variable pp is the path to the file which I didn’t use, but is an example of how to get the posix path.
Edited: also, if you have many mho files, then there might be a better way than calling do shell script many times. However, there is a limit on the amount of text you can send with one do shell script.
this was very useful. Do you know of a way that I can get the path of my new folder as an input? What I’m trying to achieve is this.
1- Copy a folder ‘‘A’’ to a new destination ‘‘B’’
2- Once the copy’s complete, look for mhl files in the new subfolder ‘‘A’’ that was copy in the ‘‘B’’ destination. and run the mhl verification
set target_folder to choose folder
tell application "Finder"
set new_folder to (duplicate target_folder) as alias
set file_list to (every file of target_folder whose name extension is "rtf") as alias list
end tell
repeat with this_file in file_list
set pp to quoted form of (POSIX path of this_file)
set cmd to "osascript -e '" & "say \"hello\"'"
do shell script cmd
end repeat
Instead of using ‘choose folder’ to get the reference every time, there are other ways to get the reference. You can hard code the path within the script. You can also write the script to ‘choose folder’ on the initial run. Etc. Note that in the script above the target folder is duplicated in place with the new name “OldName copy (n)”.
so I did try it and it’s almost working. The thing is that I need to run the mhl check only on the folder that I just duplicate. What I’m doing is duplicating one folder every day to the destination so the destination folder will have more than one subfolder in it (i.e Day01, Day 02, Day 03…).
So when I’m gonna duplicate Day 04, I just want to run mhl verification on this folder as all of the others will have been done prior.
So what you would want to do is ‘set new_folder to (duplicate target_folder to some_location) as alias’. Here the duplicate command returns a reference in new_folder to the copy in the new location. I should have used the ‘to’ parameter instead of just duplicating in place (same place as the original).
It’s confusing at the beginning with all the different type of references.
Everything seem to work but I<m getting a strange error
‘‘error “Can’t get POSIX file (alias "MacHD:Users:fcengarle:Desktop:Test_SCRIPT:Ok2:").” number -1728 from POSIX file (alias “MacHD:Users:fcengarle:Desktop:Test_SCRIPT:Ok2:”)’’
I’m copying the folder ‘‘OK2’’ to the folder ‘‘Test_Script’’. So the script seem able to get the right path but don’t know why, doesn’t seem able to get the POSIX
I was sure that I used to use ‘posix file and_something_else’. But now I need to use posix file of and_something_else. Try adding the ‘of’ as in ‘posix file of …’'.
still getting the can’t get POSIX file error. Thing is that I had to modify the script a little, think I might have screwed everything :-p
What I did if modify it so I get prompt on what source folder I want and what destination I want to copy it (here’s the script)
tell application "Finder"
set sourceFile to choose folder with prompt "Choose a folder to copy:"
set targetFolder to choose folder with prompt "And where do you want it to go?"
end tell
tell application "Finder"
try
set new_folder to (duplicate sourceFile to targetFolder) as alias
on error errMsg number errNum
display dialog "Error: " & errNum & return & errMsg
end try
update targetFolder
end tell
set input to POSIX file of new_folder
tell application "Finder" to set theFiles to (files of entire contents of folder input whose name contains ".mhl") as alias list
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with aFile in theFiles
set filePath to aFile's POSIX file
tell application "Terminal"
do script "mhl verify -f " & quoted form of filePath in window 1
end tell
end repeat
I’ve looked back through the your post and can’t find where I posted ‘posix file’. What do you need it for again? I think it just switches “:” to slashes and vice versa. Need to test that out again.
Ok, I see. Posix file is completely different from posix path. I think posix file is mainly used to create a file specification from a posix path. In unix posix paths are used and not, posix file. I think you don’t need file specifications so replace all your posix files with posix paths.
I looked again at your script now that had more time and here:
set input to POSIX file of new_folder
You set input to a posix file path which is a mac style path. then you use it in Finder and so it should work. Now here:
repeat with aFile in theFiles
set filePath to aFile’s POSIX file
tell application “Terminal”
do script "mhl verify -f " & quoted form of filePath in window 1
end tell
end repeat
You use posix file and get a mac style path, but you use it in Terminal. I think that should error. That’s where you should get the posix path instead for use in the shell script.
I did try with Posix path, but still isnt working. I did some more test and I get sooooo close but have one little part (that start to drive me crazy :-p) missing.
I did a script with the path of the folder in it just to see if it work. And it did!!!
set input to POSIX file "/Source/Ok2"
tell application "Finder" to set theFiles to (files of entire contents of folder input whose contains ".mhl") as alias list
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with aFile in theFiles
set filePath to aFile's POSIX path
tell application "Terminal"
do script "mhl verify -f " & quoted form of filePath in window 1
end tell
end repeat
Now what we need to do is passing the info of my new target folder to my ‘‘set input’’. Which I thought I could do by setting the variable ‘‘new_folder to (duplicate sourceFile to targetFolder) as alias’’ and then calling the ‘‘new_folder’’ variable as ‘set input to POSIX file new_folder’‘’. But doesn’t seem to work as, for some reasons the path comes out with ‘’:‘’ instead of ‘’/‘’
Woah…I think I got it. It seems that the problem was coming from the ‘‘set input to POSIX file of new_folder’’ I just deleted that line and simply went ‘‘tell application “Finder” to set theFiles to (files of entire contents of folder new_folder whose name contains “.mhl”) as alias list’’ and suddenly, everything is working. note quite sure I understand why or if it even make sens, but it’s a bit too late to think it through right now, need some sleep
here’s the new script that is working
tell application "Finder"
set sourceFile to choose folder with prompt "Choose a folder to copy:"
set targetFolder to choose folder with prompt "And where do you want it to go?"
end tell
tell application "Finder"
try
set new_folder to (duplicate sourceFile to targetFolder) as alias
on error errMsg number errNum
display dialog "Error: " & errNum & return & errMsg
end try
update targetFolder
end tell
tell application "Finder" to set theFiles to (files of entire contents of folder new_folder whose name contains ".mhl") as alias list
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with aFile in theFiles
set filePath to aFile's POSIX path
tell application "Terminal"
do script "mhl verify -f " & quoted form of filePath
end tell
end repeat
I think that when you’re going into subdirectories, you might want to use unix. It depends on how many files you plan to be working with in the future. There are many people here who know how to do that if you need more speed later.
Once again, thanks a lot for all the support Kel, it was really appreciated
Do you know about timeout? I’m gonna have to copy some heavy folders with a lot of files (right now I’m testing a 40 gig folder with 1200 items in it). To make sure that I don’t time out, I had a “with timeout of 500000 seconds” command. Now this is working as I don’t seem to have any event time out, but at the end of the copy (around 26 minutes) both applescript and finder seem to be “not responding” Any idea why?