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

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

Related

VBA - Macro - To Print Multiple digital signed PDF file and save it in subfolder by using "Microsoft Print to PDF" Printer

I am printing multiple digital signed PDF file into PDF via "Microsoft print to PDF" ( To Edit document) . Below mention VBA code is working perfectly. But when run this code each time, it is asking Filename & Destination folder for printed file.
My Expection:
It has to capture file name from existing saved documents file name and destination folder path we have include in VBA Code.
Please help me, How to solve this
Public Sub Print_All_PDF_Files_in_Folder()
Dim folder As String
Dim PDFfilename As String
folder = "C:\Users\Desktop\VBA\" 'CHANGE AS REQUIRED
If Right(folder, 1) <> "\" Then folder = folder & "\"
PDFfilename = Dir(folder & "*.pdf", vbNormal)
While Len(PDFfilename) <> 0
Print_PDF folder & PDFfilename
PDFfilename = Dir() ' Get next matching file
Wend
End Sub
Private Sub Print_PDF(sPDFfile As String)
Shell "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /p /h " & Chr(34) & sPDFfile & Chr(34), vbNormalFocus
End Sub
Path with spaces must be in quotes, because it is has spaces. Keys /p and /h must be separate from Program name. I check it this way:
i make this command in cmd.exe and when i see what it correct - I revrite it into macro.
Private Sub Print_PDF(sPDFfile As String)
Shell "" & Chr(34) & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & Chr(34) & " /p /h " & Chr(34) & sPDFfile & Chr(34)
End Sub
You seem to have multiple conflicts
Your command includes the command to open the Printer Dialog
/P <filename> - Open and go straight to the Printer Prompt dialog
And for "Microsoft Print to PDF" that will allow you to make the manual changes you require to the PDF then manually save to a folder or filename of your choosing.
However you say you want Acrobat to save to a known filename without that prompting. Which in turn makes me question WHY are you using Acrobat to open a PDF and re-save it as a file name without interaction ?
You could do that simply by renaming the PDF without opening it in Acrobat.
One advantage of programmatically opening a "Complex" PDF in Acrobat and Re-Printing as a "Dumber" PDF using "Microsoft Print to PDF" is it can pseudo-manically emulate much more efficient ways of flattening by using a very inefficient reprinting and for that you need to use:-
/T <filename> <printername> <drivername> <portname> - Print the file on the specified printer.
Where printername and drivername are "Microsoft Print to PDF" and portname is where you want it printed.
There are much lighter ways to process a PDF from the command line, but if you already have installed heavyweight Adobe Reader then this is the defacto standard.
[EDIT] in the comments you imply you still need to use acrobat for processing before printing to a fixed name. Then in that case, you need to run those actions first. Before saving as new PDF, prior to printing, thus you need to
get filename
make changes
save changes as new filename
send new filename to printer using:-
"C:\Full path\to\AcroRd32.exe" /T "C:\path to\Input.pdf" "Microsoft Print to PDF" "Microsoft Print to PDF" "C:\path to\Output.pdf"
The problem with batch printing, using /T = TSR (Terminate and Stay Resident), is that the window stays open waiting for the next print in the batch, and most users then add /H to hide it, then afterwards complain its not accessible so as to close at the end of the batch (which simply requires sendkeys %FX or Alt+F4 to close the open window)!
One way round that is, on the last print invoke /T without H, and then a VB focused command (object.AppActivate title) and at simplest sendkeys %FX will close the window.
If using the command line or a .cmd it is simple to use Wscript with a single line .VBS command, however in this case you are already using VBA.

Mac OS app based on Applescript

I'm new to Xcode as well as I am to AppleScript
All I need is to run an AppleScript in an Xcode application.
Bassically, the application is downloading pictures from a url list in Excel and renames the files acording the list in Excel
Here is the applescript code :
tell application "Microsoft Excel"
set filenames to value of every cell of range "K3:K200" of sheet "Image_Moves" of document 1
set URLs to value of every cell of range "L3:L200" of sheet "Image_Moves" of document 1
end tell
repeat with i from 1 to count URLs
if (item i of filenames is not missing value) and (item i of URLs is not missing value) then
set thisFname to quoted form of (POSIX path of ({path to desktop, "Image_Moves:"} as text) & item i of filenames)
set thisUrl to quoted form of item i of URLs
set status to (do shell script "curl -s -o " & thisFname & space & thisUrl)
end if
end repeat
try
tell application "Finder"
delete (every item of folder ("Image_Moves") whose name begins with "0")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
In Xcode, you can write a full application in AppleScript with the "Cocoa-AppleScript" template. Also, you can compile and execute an AppleScript with the NSAppleScript class. If your question is just about making an AppleScript executable, use Save As... in Script Editor and save your AppleScript as an application.
HTH

excel vba won't execute .bat but manually executing bat works fine

Hi I have a perfectly working bat named: start.bat
containing:
start C:\Users\*user*\Documents\*path*\hidebat.vbs
and once it is manually opened it works perfectly, meaning it opens hidebat.vbs, which opens a .bat minimized which uploads files to my cloud. Hence it's verified.
I've added
pause
to the start.bat to see what it does and when I tell excel to open the start.bat it will open cmd and display the exact command as required, but it will not execute the hidebat.vbs.
I expect that there is somehow some path constraint or environment constraint when it is run from excel that prevents it to actually reach out of that limited environment.
Within excel I have tried calling the .bat in 3 different ways with:
Dim path As String
path = Application.ActiveWorkbook.path
path = path & "\"
Dim MY_FILENAME3 As String
MY_FILENAME3 = path & "start.bat"
1.
retVal = Shell(MY_FILENAME3, vbNormalFocus)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
2.
PathCrnt = ActiveWorkbook.path
Call Shell(PathCrnt & "start.bat")
3.
Dim batPath As String: batPath = path
Call Shell(Environ$("COMSPEC") & " /c " & batPath & "start.bat", vbNormalFocus)
Does anybody have any clue on why it will not execute the .bat file, or what I could do to ensure it will run correctly?
Note. I think it is because it opens the default path, so I'm gonna tell it to "cd" to the actual path where the excel is saved and where the .bat files are.
Yes that was it, the path was set to some random/standard/working/current path by command, so I had to add:
Print #FileNumber, "cd " & path
to the excel macro
so that start.bat looked like:
cd *path*
start *path*\hidebat.vbs
Hope this helps future me's.

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