Make my applescript process specified files in the current directory? - pdf

I need to generate PDFs from word files regularly, and I'm getting tired of doing it by hand.
Manually, all I do is open a file, and click "Save as PDF". So, one would think applescript would be a nice easy way to do this. [If you have another approach than applescript, I'm open to it.]
I'm almost there, the following script works, except the full path is hard coded.
tell application "Microsoft Word"
open file "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.docx"
set doc to document "ActivityGuide.docx"
save as doc file name "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.pdf" file format format PDF
end tell
I need it to work for other people, on other machines, so the username and other parts of the path may change. If I could just do this from the current directory for the script, I'd be set.
I found this: Applescript to launch file from current folder?
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
Which works for opening an app from the current directory in Word, via Finder, but I think I need to open it with an application of "Microsoft Word", if I'm going to use "Save As". But this method of opening doesn't work if I change the application for "Finder" to "Microsoft Word".
Any suggestions welcome.
[edit: clarity]

Try this
set x to path to me
tell application "Finder" to set tFile to (file "ActivityGuide.docx" of (container of x)) as alias
set newFile to (text 1 thru -5 of (tFile as string)) & "pdf"
tell application "Microsoft Word"
open tFile
tell document 1
save as file name newFile file format format PDF
close saving no
end tell
end tell

Another thing that might be helpful to you is under System Preferences:Keyboard:Shortcuts: App Shortcuts:All Applications
Make a shortcut that is named "Save as PDF..." assign the shortcut to command+P since most applications that is the shortcut for print, then just press it twice and it will prompt you to save it as a PDF (I got that tip from David Sparks) and depending on the application it will default to where the file was opened up from. You can also set that with the application Default Folder X.
Hopefully that is a bit more of a global solution for other problems but it looks like you already have the answer to this isolated problem.

Related

Create Document in the Folder the EXE is in (VB)

I'm starting to play around with FileStream to make a text document. When you do this, you have to clarify a path. Is there a way to create the text document in the folder the EXE file is in?
(I'm asking this because this program is meant to be downloaded, so I think I can't clarify a path specific to my computer)
Thank you!
You're right, you can't bake a path into your program that is specific to your computer because then it won't work on the user's computer
Jimi makes the wise point that often programs are installed to C:\Program Files or similar and it's not automatically possible to write to subfolders in there - you'll have to get into asking the user for permission (Elevation) .. headache
Better to decide what you want the path for:
If you need a temporary path to e.g. download something to then throw it away you can call Path.GetTempFilename() or Path.GetTempPath() - the former creates a 0 byte file with a random name in the user's temp folder, and returns the path. The latter gives you the path to the temp folder so you can create your own file
If the file is to have some permanence, such as the user saving his work, you should ask the user for it. SaveFileDialog and FolderBrowserDialog are two things you can drop on your windows form and then call ShowDialog() on to show the uer a UI where they pick a path. After they OK or Cancel, you check if they OK'd or Cancel and proceed using the dialog's Filename or SelectedPath respectively (if they OK'd)
When you're writing your files it's easier not to use FileStream unless you really need to seek in the file etc. Easier to just:
System.IO.File.WriteAllText(path here, contents here)
If you have to write the contents of a string variable to a file
The best way to create a text file, would be to use CreateText method. It will create a file besides the executable program file. You can go the following way.
Dim sw as StreamWriter = File.CreateText("myfile.txt")
Dim str as String = "Your text"
sw.Write(str)
sw.Flush()
sw.Close()

Print button didn't save the PDF file at a shared path, and no error logged

I have an Excel macro that uses SAP for printing data in pdf format.
Steps:
it access the SAP transaction which provides a table with the necessary information
it press Print button (from the Menu Bar of SAP)
then Print window appears (from here it is selected Microsoft Print To PDF option and then it is pressed OK button)
Save As window appears (complete the path and filename: \S\BC....\outputName)
then Save button it is pressed
then no error happens
But if I access the path folder "\S\BC....", nothing happen, there is no file saved. BUT, if we look in "Date modified" property of the folder, it is modified in accordance with the last saving time.
Also, I mention that if I'm trying to write the pdf file on local folders, not network shared folders (example: a desktop folder: C:\Users\mariah\Test), macro works.
Also, I mention that user can Read&Write at the shared path \S\BC....\
Please help me to find the cause of this issue.
A VBA macro only does what you are allowed to do manually. Can you manually save the file in the shared path without the macro?
Is the common path really \S\BC...\ or \ \S\BC...\ ?
Regards,
ScriptMan
Solution proposed by the OP in this comment:
IT WORKS if I save the file on "C:\Users\UserName" and then cut it to shared path "S\BC...". So I've implemented code that saves the file in a path and then copy it to another path and then I deleted it from the first path (such that user never knows that I copy the file in other destination than desired destination).

Word VBA .SaveAs2 pops message (in some situations)

SharePoint 2010
Word 2010
Windows 7 Enterprise
I have a .docm file that lives in a SharePoint document library. When it is opened, a macro fires and prompts the user for a new file name and folder and then uses .SaveAs2 to save a copy of the file in the specified location as a .docx without the macro.
I cannot use SP content types with a proper .dotm as the template, since there are over 30 different file templates in the library. I have to use the .docm in a SharePoint library and then make sure that the user saves a copy of the file to their personal drive.
So I use some code in the open event. I let the user specify a folder and a file name. Then .SaveAs2 takes these parameters and saves the current file in the new path with the new name as a normal .docx file without macros. I'll spare you the details about how strFolder and strDoc are gathered. Rest assured that they exist. I have a debug.print with the full file name and it is correct.
With o
.SaveAs2 strFolder & strDoc & ".docx", wdFormatDocumentDefault
End With
The problem is that this code brings up a message saying that the file cannot be found.
Well, duh, I am trying to save the file in this location. Of course it does not exist. That's the point. (Note that the folder does exist.)
After the message box is closed, Word happily saves the file to the specified location.
Also, the message only pops up if the original file is opened in Read mode from SharePoint AND if the new file path is on a network drive.
The message does NOT pop up if
the file is opened in Edit mode (click the SharePoint file, select Edit in the next dialog), or if
the file is saved to a local drive (C:) or if
the file is opened from the File > Recent backstage dialog.
In the production system, the users will not have a choice of Edit or Read only. They will default to Read only. Also the users will not be able to save to a local C:\ drive, since the business system puts their profile and "My Documents" on a network drive (H:).
I have tried
saving the file with a different approach: using msoFileDialogSaveAs -- same message
suppressing the message with Application.DisplayAlert = False (I know but I was desperate) or wdAlertsNone. - Does not work. Message still shows
suppressing the message with Application.ScreenUpdating = False. Does not work. The message still shows.
suppressing the message with error handling On Error Resume Next or On Error Goto MyHandler but the message pops up without the error handlers being fired. The message has the blue "i" icon, so maybe it is not interpreted as an error, but as a piece of information.
How do I make the message go away?
Also, although this is not essential, it would be nice to know:
Why does Read or Edit mode matter when the file is saved to a new location?
Why does the new location of the file (network path or local path) matter when it is saved?
And why does the message come up when afterwards the file saves correctly?
After several futile attempts to change the way SharePoint serves the document in read only mode, I used the following approach to create a new Word doc, save it to the user's temp folder, copy the doc from the temp folder to the folder previously specified by the user. Now the document exists and using SaveAs does not trigger the error message.
Before the code below runs, the user has defined a file name (strDoc) and a folder for the document to be saved to.
' since we get an annoying message when trying to save to a network drive while
' in read only mode, we first create a new, empty file in the user's temp folder,
' then copy that empty file to the specified folder
' set the temp folder and full path
tempFolder = Environ("Temp")
tempPath = tempFolder & "\" & strDoc & ".docx"
' create a new document
Documents.Add DocumentType:=wdNewBlankDocument
ChangeFileOpenDirectory tempFolder
' save to temp folder and close
With ActiveDocument
.SaveAs2 tempPath, wdFormatDocumentDefault
.Close
End With
' copy from temp folder to previously defined destination
FileCopy tempPath, fullPath
' delete the temp file
KillFile = tempPath
' finally, save the contract over the empty file
With o
.SaveAs2 fullPath, wdFormatDocumentDefault
End With
Here's a shot in the dark based on some googling and similar experience:
http://blogs.technet.com/b/wordonenotesupport/archive/2009/02/07/word-2007-file-save-errors-an-error-is-displayed-when-attempting-to-save-a-word-document.aspx
The relevent bits:
"<path and file name>" cannot be found. Check your spelling, or try a different path.
<path and file name> is currently in use. Try again later.
Word cannot complete the save to due to a file permissions error.
You might see the aforementioned errors if Word 2007 is having a
conflict with antivirus software. Most of these conflicts have been
addressed by the antivirus software manufacturers, but you must
download and install updates or patches for the antivirus software
(not just new virus definitions) to resolve the issue. See the
software manufacturer's web site for possible updates and for
knowledge base articles concerning configuration issues.
...and then from McAfee describing a related issue:
https://kc.mcafee.com/corporate/index?page=content&id=KB75449
Most forum discussions I found implicate server-side realtime anti-virus scanning - something about the interplay of behavior between the saving mechanism of Office 2010 not playing nice with live, access-based file scanning.
Even if this doesn't solve your issue, hope it helps!

Photoshop Scripting with Applescript -- will not open images without a prompt

tell application "Adobe Photoshop CS5.1"
set myFile to (choose file) as string
open file myFile
end tell
This results in 2 separate dialogs opening asking me what file to open.... It doesn't matter what I select first, it asks a second time and opens the second.
What I want to open is a PDF, so I really want:
tell application "Adobe Photoshop CS5.1"
set myFile to (choose file) as string
open file myFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1} showing dialogs never
end tell
Result:
Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop
All right a more direct approach:
tell application "Adobe Photoshop CS5.1"
set myFilePath to alias "other: PREPRESS SAVE:GAMES:3Sudoku:20120213pzsud-v:sudoku 0213.pdf"
with timeout of 300 seconds
open myFilePath as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1}
end timeout
end tell
The file exists or the setting of the alias would fail. So what do I get?
"Error: -43. Adobe Photoshop CS5.1 got an error: File some object wasn’t found."
I had asked over on the Adobe forums, with no response. The full code I'm really trying to use is there if you're interested. I reduced it to basics to figure out what the problem is.
Is there a setting somewhere that I am missing? I'm not sure what is wrong, the example out of the
There are horrible issues with The Photoshop 5 dictionary. One idea is to try and run it in 32-bit mode which 'solves' a lot of issues. Please digest further in http://forums.adobe.com/message/2822670#2822670?tstart=0#2822670
Might be a CS5 issue, but I honestly don't know.
This worked fine for me, but with CS4:
tell application "Adobe Photoshop CS4"
set myFile to (choose file) as alias
with timeout of 300 seconds
open myFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1}
end timeout
end tell
The only real difference (that I can see) is that I alias the chosen file, and call open ... - not open file ...

Aspell Dictionary can't be opened

When I try to add a word or to ignore all, Aspell throws the error 'the file "C:/Program Files (x86)/Aspell/en.pws" cannot be opened for writing'. Anyone know what is causing this problem and how to fix it?
Ensure that the security settings of the "C:/Program Files (x86)/Aspell" folder for the current user allow for modifying and writing.
I had the same issue, and this cleared it right up.
Here is some help to the missing en.pws and en.prepl files. I have no idea why the developers failed to provide this simple solution for windows.
Go to the directory of "Aspell", which should have been installed as a sub-directory of NotePad++.
Right click in the main Aspell directory, choose "New" then choose "text document".
Rename the new text document as "en.pws" (without quotes) removing the "txt" extension, press enter key.
Repeat, make another new text document and rename it to "en.prepl".
Open up en.pws in NotePad++
copy and paste the following into en.pws, exactly as below;
personal_ws-1.1 en 2
µÄ
(Note: the µÄ symbols are below, on a new line below personal_ws-1.1 en 2)
Go to the Menu in NotePad++, click "Encoding" and make sure that "Encoding ANSI" is chosen (Dot beside it).
Save file and close file.
Open the en.prepl file in NotePad++
Copy and paste the following into that file;
personal_repl-1.1 en 0
Same, make sure the "Encode ANSI" is chosen.
Save and close file.
Note: you must close each file before using NotePad++ Spell Checker and "learn" button. Else it corrupts the file and you have to redo it.
NotePad++ will now work and you can save words to the ".pws" file.
Possible locations of GNU Aspell custom words file:
%localappdata%\VirtualStore\Program Files\Aspell
%programfiles%\Aspell
For English for example the custom words file is named en.pws (file name = language code) and the very first line must look like:
personal_ws-1.1 en 286
Where "en" is the file name (=language) and 286 are the number of words (lines) in that file. Last line must is always empty. GNU Aspell does not allow words ending with a number (e.g. Beta1). If you have such a word in your pws file Aspell or the application that uses it (e.g. Notepad++) may crash/hang ...
You may have to delete your en.pws file and create a new one (e.g. using Notepad++). File format as described above, otherwise GNU Aspell will fail to work.
I took the read only off of "C:/Program Files/Aspell" folder.
N++ still says
GNU Aspell
Error:
The file "C:/Program Files/Aspell/en.pws can not be opened for writing.
The english .50 dictionary install does not do a proper install into your C:/Program Files/Aspell directories. I had to open the Installer .EXE in WinRAR and copy the folders.
Should I copy the English dictionalry installer into my C:/Program Files/Aspell folder and install from there?
"Ensure that the security settings of the "C:/Program Files (x86)/Aspell" folder for the current user allow for modifying and writing"
"I took the read only off of "C:/Program Files/Aspell" folder. [The error message remains]"
It's not a read-only problem, but permissions. In Security give "Users" "Full control" over the .prepl and .pws files.
The Answer by "sth" and "Mark" is good, but it still has permissions issues in Windows 10. Instead of creating the two files in the Program Files folder for Aspell, create and initialize them on your desktop first; that will give your account full control on the files. (Also, set the encoding before you paste the initializing text.) Then move the two files into the Program Files folder for Aspell, and they will retain the full write permissions of your account without messing with the overall permissions of the Program Files folder for Aspell.