AppleScript: Create new file using custom New Text File dialog? - dialog

I just finished creating a script that would let me create a blank, untitled file in the currently focused finder window then focus on the file to quickly rename.
I'm looking to make use of the built-in New Text File dialog, but without File Format and Encoding.
Is there any way to create this dialog box?
Here is how I make a new file in the current location:
on run {input, parameters}
try
tell application "Finder"
set wasFolder to true
set selectedFolder to first item of input
set selectedType to (kind of (info for selectedFolder))
if selectedType is not in {"Folder", "Volume"} then
set selectedFolder to container of selectedFolder as string
set wasFile to false
end if
end tell
tell application "Finder"
activate
set theFile to make new file at folder selectedFolder
if selectedFolder as string is equal to (path to desktop as string) then
select window of desktop
set selection to theFile as alias
else
reveal theFile
end if
end tell
tell application "System Events"
tell process "Finder"
if wasFolder then delay 0.4
keystroke return
end tell
end tell
on error errMsg
display dialog "Error: " & errMsg
end try
end run

You can try something along the lines of...
set userResonse to text returned of (display dialog "Enter a name..." default answer "response" buttons {"Cancel", "OK"} default button "OK")
tell application "Finder"
if exists Finder window 1 then
make new file at (target of front Finder window as text) with properties {name:userResonse}
else
make new file at (path to desktop) with properties {name:userResonse}
end if
end tell

You can now get New File Menu from the Mac App Store. It comes with a customizable preferences menu that lets you create new files with specific extensions.

Related

Print to "Microsoft Print to PDF" with a specific file name

I have a number of questions on this topic:
If I print using Application.ActivePrinter = "Microsoft Print to PDF on Ne01:" then how can I pass a specific file name and path to the printer? (Rather than having it prompt me?)
Since I want others to use this file, how can I check that Microsoft PDF is always on Ne01? What if theirs is on a different port?
Many suggest using a Findprinter command, but I always get the sub or function not defined error
Many suggest using the built-in ability to save as a PDF, instead of printing to PDF, but I find the resulting PDF has huge margins and the wrong size (not A4)
You can save Excel sheets as PDF directly. Try below macro. Change C:\ to save your file to your desired location.
Sub SavePDF()
Dim pdfName As String
pdfName = "MyFileName"
Sheets("MyPDFsheet").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\" & pdfName & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, _
OpenAfterPublish:=False
End Sub
PDF printouts go to a "Port" that port can have a filename same as any printing device. The user normally is tasked with select device/folder/portname.pdf thus there is a virtual PORTPROMPT: device (that is set via the print prompt dialog)
A) Set a duplicate driver to a port as a fixed filename in a watch folder. see Print to PDF with powershell while suppressing the "Save As" Prompt and https://stackoverflow.com/a/69169728/10802527
A) You cannot, this Windows 11 just Like Other Windows versions currently does not have an MS PDF printer, it is a windows optional extra I am not allowed to install.
A) Too variable, Default 1st NEtwork printer may be a Fax or in this case nothing so should return NULL
A) Save should have options. see https://superuser.com/questions/1064214/create-pdf-from-an-excel-file-without-white-margins for other suggestions, However for PDF Print there are many reasons for no fixed size, Windows will keep changing printers to "last used" unless told other wise, and last used can be any paperkind or margins.
However, In my case I frequently set default to my region (A4 Portrait) and as often as I can I set "No margins" but windows Edge will frequently reset that to big "Default". Perhaps I should set default to A4zerOmargin

AppleScript - How to append text to end of a file

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

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,

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

Resources