AppleScript - How to append text to end of a file - text

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

Related

Use Automator to read a text file and copy contents to another Folder

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,

How do you check if a file is in use?

I have a desktop application that writes an XML file out. If the XML file is open in a text editor (from a prior running of the script), when Livecode saves the file out, the XML data is not changed.
The actual line I'm using to write the file:
put tOutputData into URL ("file:" & tFilePath)
I can see that using "open file-->write-->close file" logic instead of "URL (file:)" will over-write the data if the output file is already open in a text editor. However, the text editor will show the "old" data until the file is reloaded.
How do I ask the file system if the file is already in use before attempting to write to the file in Livecode?
Check the result to see if there was a problem accessing the file:
put tOutputData into URL ("file:" & tFilePath)
put the result into rslt
if rslt is not empty then
beep
answer error rslt
end if

Run script on specific file type

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

Using applescript to move files whose names appear in an excel spreadsheet

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

Closing Excel from Applescript without generating errors

I have an Applescript that is designed to convert .xlsx files into .csv format, but it's having problems closing Excel after it's done processing, such that the next time Excel opens it gives the error message "excel saved changes to the file before the application quit unexpectedly". If I run the Applescript twice in a row, this error message causes the second execution to crash. Is there a way to either: close Excel more gently, so that it doesn't register the error; or tell Applescript to ignore the error and proceed with the rest of the program?
For reference, here is a simple Applescript that has the same problem (replace "etc.xlsx" with a path to some .xlsx file):
set fullpath to alias POSIX file "etc.xlsx" as text
tell application "Microsoft Excel"
activate
open fullpath as text
close active workbook
quit
end tell
I don't get the error you describe. You have some confusing file path stuff going on. Maybe that's your problem. To get the string path to a file use the following code and put the result path in for your fullpath variable.
(choose file) as text
Then here's the code to work with it.
set fullpath to "some:path:test.xlsx"
tell application "Microsoft Excel"
activate
open file fullpath
close active workbook
quit
end tell
It works without error.

Resources