I'm fairly new in the work of applescript and was wondering if someone can help me with this.
I'm currently writing script that will do a lot of things, one of them will be looking in a folder for files that have the .mhl extension to run a terminal command on them. I've paste the part of my script bellow. Now, this seems to work fine except fore one little detail. the .mhl files need to be at the first level of my folder. The problem is that my folder contains subfolder which contains the .mhl. Is there a way I can tell this script to look in the entire folder (subfolder include)?
Thanks a lot
set input to "/Source"
tell application "System Events" to set theFiles to POSIX path of (files of folder input whose name contains ".mhl")
repeat with i in theFiles
set filecount to 1
tell application "Terminal"
activate
do script "mhl verify -f " & POSIX path of i & ""
end tell
end repeat
Probably easier to use the find command but here is an AS approach...
set input to POSIX file "/Source"
tell application "Finder" to set theFiles to (files of entire contents of folder input whose name extension = "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
Related
I'd like to have a script that I can run where it basically takes the .rdp file that I have selected in finder, and append a line of text to the end of it.
e.g
I download a .rdp file to use in Microsoft Remote Access and to speed up my workflow I'd like to append the text 'Use Multimon:i:1' at the end before I launch it so that I don't have to open the preferences each time.
I'm not too familiar with AppleScript so would appreciate any advice on how to achieve this.
Thanks!
It is my understanding that .RDP files are saved in plain text format. If this is the case, using the do shell script command in an AppleScript, appending text to a file is fairly simple. This following AppleScript code should work for you.
Paste this following code into a new Script Editor.app document. Then
with your .RDP file currently selected in Finder, run the code in Script Editor.app and it will append the text to your file.
property addText : "Use Multimon:i:1"
tell application "Finder" to set selectedFile to POSIX path of ((get selection) as alias)
do shell script "echo " & quoted form of addText & " >> " & quoted form of selectedFile
This currently works for a selected file with the extension 'txt'. Test it and if it works for you, edit it to your desired extension.
The 'if' statements are to ensure that you don't accidentally append the text to a binary file which could corrupt that file. The 'return' means that your text will appear on its own line. If you don't want that, remove 'return & '. For details on 'open for access', please see the Language Guide: Commands Reference. On the same page, you can find 'close access'.
tell application "Finder"
set tFile to selection as alias
if name extension of tFile is "txt" then
set corR to true
else
display alert "Are you sure you've selected the correct file?"
set corR to false
end if
end tell
if corR is true then
set ab to open for access tFile with write permission
write return & "Use Multimon:i:1" to ab starting at eof
close access ab
end if
When I switched to MacOS Mojave, I was no longer able to save an Excel document as a PDF to a specific location using a macro.
I have had to save it to /Library/Group Containers/UBF8T346G9.Office/ and then move it to the Desktop by calling a script stored in /Library/Application Scripts/com.microsoft.Excel/.
This has worked for months. Someone who uses this macro/script combo has all of a sudden been unable to do so. No other user has a problem, some having the same combination of Excel and MacOS (16.29 and 10.14.6 respectively).
When she tries to run the macro, it throws
Invalid procedure call or argument (Error 5)
specifically on the line that calls this script.
There is another script at the beginning of the macro that does not take an argument and is working. I used MsgBox to see what string was being passed to AppleScriptTask, plugged it into the script using Script Editor, and ran it without issue.
Here is the AppleScript:
ExistsFile(sPath)
on ExistsFile(sPath)
set sPath to sPath as POSIX file
tell application "Finder" to set sPath to file sPath
set dest to (path to desktop)
move sPath to dest without replacing
end ExistsFile
Here is the call from the macro:
result = AppleScriptTask("moveToDesktop.scpt", "ExistsFile", sPath & strJobNumber & " Cover Page.pdf")
I tried the following based on what I have seen on other forums:
Set the filename to a variable and passing that to AppleScriptTask
Removing the extension in the script, removing the extension in the folder, and combinations of those two
In MacOS Mojave and newer, you'll need to do the following:
Open System Preferences - Security and Privacy - Privacy Tab - Automation - Excel ~ check off Finder.
Sounds like it could be a permissions error if it fails to run for one user only. This page has all the requirements to run the AppleScriptTask command. Possible solution could be to enable the correct permissions for the script to run using chmod command from terminal. This will provide permission for root and current user. If this doesn't work, double check that all the requirements are met on the user's system.
cd <path to script file>
sudo chmod 755 moveToDesktop.scpt
Its the spaces in the parameters you are passing to the AppleScript. You have at least one one before "Cover Letter.pdf" (and then one between "Cover" and "Letter"), probably in other cells as well that you are using to create the parameter.
I had the same problem as well as it was driving me mad that the AppleScript itself was working fine when feeding it the combined parameter directly.
I am trying to process a number of files in Applescript. This includes moving
each file to a work directory for processing
When my script executes
tell application "Finder"
move file to workdir
end tell
It always causes a sound (just like moving a file using the finder GUI dos)
How can I prevent this? The script will likely run for many hours and constant dinging from this script would be disturbing. I cannot turn sound off completely as other things may need to alert me
Since you explicitly stated "I cannot turn sound off completely as other things may need to alert me" in your OP, I'd suggest you use the move command from System Events's Disk-Folder-File Suite, as it does not make any sound using this method.
move v : Move disk item(s) to a new location.
move disk item : The disk item(s) to be moved.
to location specifier : The new location for the disk item(s).
→ disk item
Example:
set thisItem to "/path/to/disk item"
set thisFolder to "/path/to/folder"
tell application "System Events"
move disk item thisItem to thisFolder
end tell
You can disable the sound through a system file named "Volume Mount.aif". A non-destructive way to do this is to move the file to another location. Root permissions are needed to move the file.
To execute this command you will also need to disable system integrity protection:
https://developer.apple.com/library/content/documentation/Security/Conceptual/System_Integrity_Protection_Guide/ConfiguringSystemIntegrityProtection/ConfiguringSystemIntegrityProtection.html
You can move it by typing the following command into terminal:
sudo mv /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume\ Mount.aif ~/Desktop/
To move it back:
sudo mv ~/Desktop/Volume\ Mount.aif /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/
A restart is required for the sound to work again.
I have a text file that contains the file names of about 500 pictures.
I would like to use Automator to:
Read the text file
Get a list of the names
Look in a 'repository folder' with about 1000 pictures
Copy the corresponding picture to a new folder.
Is that possible with Automator on Mac?
Here is what I have attempted, however it doesn't work.
Here are the Automator Actions, in order:
This code inside of the Run AppleScript Action should do the trick:
on run {input, parameters}
set imgDestination to input's item 2
set imgSource to input's item 1
set imgNameFile to choose file with prompt {"Select file of image filenames:"}
set imageList to every paragraph of (read imgNameFile)
repeat with eachName in imageList
tell application "Finder"
set targetImageFile to item 1 of (get every file in folder imgSource whose name = (eachName as text))
duplicate targetImageFile to folder imgDestination
end tell
end repeat
return input
end run
I could not devise how to get Automator to read the text file, and remember it while choosing the folders as well, so I added the choosing of the file with the image filenames as part of the AppleScript.
Good luck,
I have a folder with about 4000 image files. I need to select some of those files, based on whether their names appear in an excel spreadsheet, and copy them to a new folder. If possible, I would also like to tag them with a color tag so I know which files in the original folder were moved and which were not. I got the code below from here.
tell application "Microsoft Excel"
tell worksheet 1 of active workbook
set fileList to value of used range
end tell
end tell
set rootFolder to "path/to/sourcefolder" as POSIX file
set filesToMove to {}
repeat with thisItem in fileList
try
set end of filesToMove to alias (rootFolder & thisItem)
on error
display dialog "File " & thisItem & " is not in the folder"
end try
end repeat
tell application "Finder"
move filesToMove to folder "path/to/destination" as POSIX file
end tell
When I run it, it's unable to find any of the files. I think the issue might have to do with using / in my file path instead of : but I don't know how to set a path to an external drive with the applescript syntax. Using the same variable declaration, set rootFolder to "path/to/sourcefolder" as POSIX file, and then telling finder to open rootFolder works fine.
I also need the script to have more flexibility - not all files have the same naming conventions, and so I need it to search for any files whose names contain the identifiers from the spreadsheet, but may not match it exactly. I.E. the spreadsheet entry may be "00103", but the file name is "PX00103KL.jpg."
In the meantime, I've created an automator workflow that moves and tags the files exactly the way I want, but only after I manually enter the search string. If I could combine those two ideas, of iterating through the spreadsheet entries in excel and using each in turn as input for the automator workflow, that would be ideal.
Thanks for any help!
I don't have Excel, but assuming that you can get a list of strings from it, here is a bit of code that will find all files in a target folder that contain the string(s), set a label to it and then copy it. Change appropriately.
It uses the unix command line tool for spotlight, mdlist to find the files. Trying to do it purely in applescript seems to be painful. The shell command when stitched together is something like this:
mdfind -onlyin ~/Documents/ "kMDItemDisplayName == '*somestring*'"
Here is the code:
set stringList to {"144", "g_"}
set rootFolder to "/Users/blah/Downloads/" as POSIX file
set destFolder to "/Users/blah/Downloads/test" as POSIX file
set filesToMove to {}
set mdAtr to "\"kMDItemDisplayName == '*"
repeat with thisItem in stringList
set sPath to quoted form of POSIX path of rootFolder
set sName to mdAtr & thisItem & "*'\""
set sCmd to "mdfind -onlyin " & sPath & " " & sName
set sResults to (do shell script sCmd)
set fList to (every paragraph of sResults) as list
repeat with i in fList
set f to (POSIX file i) as alias
tell application "Finder"
set label index of f to 3
#copy file f to folder (destFolder as alias)
end tell
end repeat
end repeat