How to use "save as" command in AppleScript with MS Word - ms-word

I want to convert MS Word document (docx) to PDF with AppleScript.
I tried the code below with Script Editor.
(MS Word document is opened beforehand.)
tell application "Microsoft Word"
activate
set outpath to "Documents/test.pdf"
set activeDoc to active document
save as activeDoc file name outpath file format format PDF
end tell
and failed with the error:
The active document doesn't understand "save as" message.
number -1708 from active document
Versions
macOS Mojave 10.14.4
Microsoft Word for Mac 16.27 (19071500)
Script Editor 2.11 (203.1), Apple Script 2.7
With PowerPoint
The code below worked. It successfully saved test.pdf under ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents.
set outpath to "Documents/test.pdf"
tell application "Microsoft PowerPoint"
activate
save active presentation in outpath as save as PDF
end tell

outpath is invalid anyway and it must be an HFS path
set outpath to (path to documents folder as text) & "test.pdf"
tell application "Microsoft Word"
activate
set activeDoc to active document
save as activeDoc file name outpath file format format PDF
end tell

Related

Powershell vs GUI shortcuts

I have a file named new.txt. Using GUI I can create a shortcut, for example, "new.lnk". When I click on "new.lnk" file, Notepad is opened with the contents of "new.txt" file. When I create the shortcut using PowerShell
NEW-ITEM -TYPE SYMBOLICLINK -TARGET "NEW.TXT" "NEW.LNK"
I can see the contents of the file using
CAT "NEW.LNK"
but the shortcut file is not working in the GUI: it does nothing.
I expect to see the contents in Notepad editor. The properties of the file created using GUI and PowerShell are the same, except for "Start in" information: blank when the short cut is created using PowerShell and with the path file directory when using GUI.
Symbolic link (symlink) is not the same as Windows shortcut. A symbolic link is created on file system level - it says "here's a file with such filename, but the content is actually in this other file". It's size is 0 Bytes, as it just points to other file.
It would be more proper to name the file "new-linked.txt" instead of "new.lnk".
A shortcut ".lnk" is a separate file that is interpreted by Windows shell. It contains a path to the target file (among other additional properties). If you create shortcut from UI and then try cat my.lnk, you'll see the content of the shortcut file itself, not the target file.
For creating a Windows shortcut from Powershell, see How to create a shortcut using PowerShell.

LibreOffice Command Line Convert CSV to DBF

I am working on a workflow where I need to be able to batch convert CSV files into DBF files. The data in the CSV files can all be output as TEXT fields and then saved as a DBF. I have tried to get any output possible using the command line in both Mac OS and Windows 7. While I was a little more successful in Windows, I was not able to get any conversion.
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to dbf --
infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2
/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2
/68/2/69/2/70/2/71/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\*.csv --outdir C:\Users\jdavidson\desktop\complete
So, I needed to have an --infilter on the command line to be able to do the conversion, but now it will only process 1 file and not batch file when using *.csv So, my next hurdle is to get over that.
The below helps to explain --infilters in more detail:
https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options#Filter_Options_for_Lotus.2C_dBase_and_DIF_Filters
Any easier way to find the filter sequence you need is by following the below listed steps:
Open the CSV Manually using LibreOffice and Specify Every Detail in the import Wizard
Run the below macro and it will show you the filter options in an input box, so you can copy the string in to your own code
Sub showFilterOptions
Dim args(),i%
args() = thisComponent.getArgs
for i = 0 to uBound(Args())
if args(i).Name = "FilterOptions" then inputbox "","",args(i).value
next
End Sub
Credit to code: https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=14018
The parameters need to be reversed. From https://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/:
soffice --headless --convert-to <TargetFileExtension>:<NameOfFilter> file_to_convert.xxx
Using the old windows command prompt:
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --headless --convert-to dbf C:\Users\jdavidson\Desktop\DBFCONVERT\test.csv
This will create test.dbf in the current directory, which is perhaps C:\Users\jdavidson or, if running as administrator, C:\Windows\system32. To print the working directory, enter pwd, and then dir to see the file.
In powershell, add "& " in front to execute LibreOffice. Then enter gci or ls to see the file.
Be sure that a LibreOffice process is not already running in the background.
I was able to get this to work using the below code:
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete
Please note, that I needed to use DBF and include an --infilter parameter field
There are two websites in my original posting that shows helpful scripts and infilter information to help other users

AHK code to locate the residing folder and highlight the active file

It’s often requires to quick locate the folder location of open active file and highlight or select the active file while working on different software applications. Quick locating the residing folder needed for finding related files in same folder, rename of the opened files or residing folder or move the file to different related folder. Current options require navigating through the loads of folders to find and locate the specific folder where it’s buried with bunch of similar other files (similar to find needle in a haystack). Microsoft Office suite has built-in feature named “document location” which can be added to quick access toolbar. But it only allow to see the folder location or full path but no single click command or key available (AFAIK) to conveniently jump to that locating folder and highlight/identified the opened file so that further operation (e.g. rename, move) could be done on that specific file/folder. This is also the case for other software applications where native program have options to get full path but no way to jump to the specific file/folder. Considering one of Microsoft Office suites application (e.g. word) as test cases the processes I could imagine as follows;
1 Get the full path (D:\Folder\Subfolder\Mywordfile.docx) of currently opened word document
2 Close the file
3 Explorer command to select and highlight the file in folder given full path (process 1)
Operation on file/folder as desire manually and double click to return to file operating applications (e.g. word).
In my assessment for Implementation of above tasks following are possibilities
Task 1 Microsoft Word has a built-in function called "document location" to get the full path of the opened document and its currently possible to copy the file path in the clipboard.
Task 2 Close the file (Ctrl+W or Ctrl+F4)
Task 3 AHK code for Explorer command to select the file for a given full path (available in Task 1)
I am facing difficulties in Task 3 and I tried each of these but so far no luck
Clipboard := “fullpath” ; Full path (D:\Folder\Subfolder\Mywordfile.docx ) copied from Word
Run explorer /e, “Clipboard”
Run %COMSPEC% /c explorer.exe /select`, "%clipboard%"
So far above explorer command only take me to my documents folder not in the specific folder location (path in Task 1). I am curious know what would be the right explorer code to select the file for a given full path in clipboard. Appreciate for supporting AHK code or better way to do this task. Thank in advance.
I'm not clear on why your sample code doesn't work. I suspect it's because of the extra characters.
After running this command Windows Explorer will be open and have the desired file selected (if it exists).
FullPathFilename := "e:\temp\test.csv"
Explorer := "explorer /select," . FullPathFilename
Run, %Explorer%
I don't know if you tried the other approach, but I think this is simpler and shorter:
1) Store the full path of the document in a string: oldfile = ActiveDocument.FullName
2) SaveAs the document with ActiveDocument.SaveAs
3) Delete the old file with Kill oldfile
All this is from VBA directly, no need to use Explorer shell. The same exists for the other applications.
Here is a fully working code for the Word Documents:
Sub RenameActiveDoc()
Dim oldfile As String
Set myDoc = ActiveDocument
'1) store current file
oldfile = myDoc.FullName
'2) save as the active document (prompt user for file name)
myDoc.SaveAs FileName:=InputBox("Enter new name", "Rename current document", myDoc.Name)
'3) Delete the old file with
On Error GoTo FileLocked
Kill oldfile
On Error GoTo 0
Exit Sub
FileLocked:
MsgBox "Could not delete " & oldfile, vbInformation + vbOKOnly, "File is locked"
End Sub
With contribution of Ro Yo Mi I am able to come up with following solution. However I am assuming that there might better solution to this task.
;;; Customize Document Location (Choose form All Commands) in Quick Access Toolbar and get its position (#4 for my case)
#If WinActive("ahk_class OpusApp") || WinActive("ahk_class XLMAIN") || WinActive("PPTFrameClass")
#p:: ;Close Word/Excel/PowerPoint Document and Locate in Explorer Folder Location
clipboard = ;empty the clipboard
Send !4 ; Select full path while document location at #4 position in Quick Access toolbar
Send ^c ; copy the full path
ClipWait ; waits for the clipboard to have content
Send {esc}
Send, ^{f4} ; Close opened document only but keep Word/Excel/PPT program running
Explorer := "explorer /select," . clipboard
Run, %Explorer%\
return

Command `libreoffice --headless --convert-to pdf test.docx --outdir /pdf` is not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have to wait for long after using this LibreOffice command which is intended to convert DOCX into PDF:
libreoffice --headless --convert-to pdf test.docx --outdir /pdf
But I'm getting no response and no error. No file is converted. It looks like terminal is hanging.
So, is there is any way to track the error?
I would first of all try it with giving absolute paths to the command.
I suspect that the libreoffice binary does not work -- you have to locate the soffice binary and see if that works.
Then, your --convert-to pdf is not sufficient. It needs to be:
--convert-to pdf:writer_pdf_Export
Be sure to follow exactly this capitalization!
Next, the command will not work if there is already a LibreOffice GUI instance up and running on your system. It is caused by bug known since 2011. Add this additional parameter to your command:
"-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"
This will create a new, separate environment which can be used by a second, headless LO instance without interfering with a possibly running first GUI LO instance started by the same user.
Also, make sure that the --outdir /pdf you specify does exist, and that you have write permission to it. Or, rather use a different output dir. Even if it is just for the first testing and this debugging round:
$ mkdir ${HOME}/lo_pdfs
Hence:
/path/to/soffice \
--headless \
"-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}" \
--convert-to pdf:writer_pdf_Export \
--outdir ${HOME}/lo_pdfs \
/path/to/test.docx
This works for me on Mac OS X Mavericks 10.9.5 with LibreOffice v4.4.3.2 (using my specific path for the binary soffice which will be different for you anyway...).
If all this doesn't work:
It may be a problem with the specific DOCX file you try the command with... So create a very simple DOCX document of your own first. Use LibreOffice itself for this. Write "Hello World!" on an otherwise empty page. Save it as DOCX.
Try again. Does it work with the simple DOCX?
If it again doesn't work, repeat step 7, but save as ODT this time.
Repeat step 8, but make sure to reference the ODT this time.
Last: Use full path to soffice, to soffice.bin and to libreoffice and run each with the -h parameter:
$ /path/to/libreoffice -h
$ /path/to/soffice -h
$ /path/to/soffice.bin -h
Do you even get an output here?
For which one of the three binaries/symlinks?
Record the outputs.
Tell us your outputs!!!
Compare them to the command line you used:
Are there any changes in parameter names, capitalizations, number of dashes used, etc.??
For comparison, my own output is here:
$ /Applications/LibreOffice.app/Contents/MacOS/soffice -h
LibreOffice 4.4.3.2 88805f81e9fe61362df02b9941de8e38a9b5fd16
Usage: soffice [options] [documents...]
Options:
--minimized keep startup bitmap minimized.
--invisible no startup screen, no default document and no UI.
--norestore suppress restart/restore after fatal errors.
--quickstart starts the quickstart service
--nologo don't show startup screen.
--nolockcheck don't check for remote instances using the installation
--nodefault don't start with an empty document
--headless like invisible but no userinteraction at all.
--help/-h/-? show this message and exit.
--version display the version information.
--writer create new text document.
--calc create new spreadsheet document.
--draw create new drawing.
--impress create new presentation.
--base create new database.
--math create new formula.
--global create new global document.
--web create new HTML document.
-o open documents regardless whether they are templates or not.
-n always open documents as new files (use as template).
--display <display>
Specify X-Display to use in Unix/X11 versions.
-p <documents...>
print the specified documents on the default printer.
--pt <printer> <documents...>
print the specified documents on the specified printer.
--view <documents...>
open the specified documents in viewer-(readonly-)mode.
--show <presentation>
open the specified presentation and start it immediately
--accept=<accept-string>
Specify an UNO connect-string to create an UNO acceptor through which
other programs can connect to access the API
--unaccept=<accept-string>
Close an acceptor that was created with --accept=<accept-string>
Use --unnaccept=all to close all open acceptors
--infilter=<filter>[:filter_options]
Force an input filter type if possible
Eg. --infilter="Calc Office Open XML"
--infilter="Text (encoded):UTF8,LF,,,"
--convert-to output_file_extension[:output_filter_name[:output_filter_options]] [--outdir output_dir] files
Batch convert files.
If --outdir is not specified then current working dir is used as output_dir.
Eg. --convert-to pdf *.doc
--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc
--convert-to "html:XHTML Writer File:UTF8" *.doc
--convert-to "txt:Text (encoded):UTF8" *.doc
--print-to-file [-printer-name printer_name] [--outdir output_dir] files
Batch print files to file.
If --outdir is not specified then current working dir is used as output_dir.
Eg. --print-to-file *.doc
--print-to-file --printer-name nasty_lowres_printer --outdir /home/user *.doc
--cat files
Dump text content of the files to console
Eg. --cat *.odt
--pidfile file
Store soffice.bin pid to file.
-env:<VAR>[=<VALUE>]
Set a bootstrap variable.
Eg. -env:UserInstallation=file:///tmp/test to set a non-default user profile path.
Remaining arguments will be treated as filenames or URLs of documents to open.
Add one more argument to your command line to enforce the application of an input filter when soffice opens your DOCX file:
--infilter="Microsoft Word 2007/2010/2013 XML"
or
--infilter="Microsoft Word 2007/2010/2013 XML"
--infilter="Microsoft Word 2007-2013 XML"
--infilter="Microsoft Word 2007-2013 XML Template"
--infilter="Microsoft Word 95 Template"
--infilter="MS Word 95 Vorlage"
--infilter="Microsoft Word 97/2000/XP Template"
--infilter="MS Word 97 Vorlage"
--infilter="Microsoft Word 2003 XML"
--infilter="MS Word 2003 XML"
--infilter="Microsoft Word 2007 XML Template"
--infilter="MS Word 2007 XML Template"
--infilter="Microsoft Word 6.0"
--infilter="MS WinWord 6.0"
--infilter="Microsoft Word 95"
--infilter="MS Word 95"
--infilter="Microsoft Word 97/2000/XP"
--infilter="MS Word 97"
--infilter="Microsoft Word 2007 XML"
--infilter="MS Word 2007 XML"
--infilter="Microsoft WinWord 5"
--infilter="MS WinWord 5"
Update
LibreOffice cannot only convert DOCX to PDF on the command line:
It can process any input format which it can open and "read": DOC, XLS, XLSX, PPT, PPTX, ...
It can convert to any output format which it can "write": DOC, XLS, XLSX, PPT, PPTX, ...
Of course, results will never be perfect, and sometimes they will even be un-acceptable to you.
Re. output as PDF:
To control, which LibreOffice component generates PDF output, you can use these variants:
--convert-to pdf:writer_pdf_Export
--convert-to pdf:calc_pdf_Export
--convert-to pdf:draw_pdf_Export
--convert-to pdf:impress_pdf_Export
--convert-to pdf:writer_web_pdf_Export
Re. input which is not DOCX:
To enforce infilters for non-DOCX input formats, you could use (list is not complete):
--infilter="HTML Document" # for HTML input
--infilter="MediaWiki" # for MediaWiki input
--infilter="Text CSV" # for CSV spreadsheet input
--infilter="Microsoft PowerPoint 2007/2010 XML" # for PPTX input
--infilter="Microsoft PowerPoint 97/2000/XP" # for PPT input
--infilter="Windows Metafile" # for WMF input
--infilter="Enhanced Metafile" # for EMF input
--infilter="Scalable Vector Graphics" # for SVG input
--infilter="Microsoft Excel 2007/2010 XML" # for XLSX input
--infilter="Microsoft Excel 97/2000/XP" # for XLS input
--infilter="Microsoft Excel 95" # for some XLS input
--infilter="Microsoft Excel 5.0" # for some XLS input
Re. output which is not PDF:
To convert to specific output formats, you could use (list not complete):
--convert-to html:HTML
--convert-to html:draw_html_Export # force "Draw" to generate the HTML
--convert-to mediawiki:MediaWiki_Web # generate MediaWiki output
--convert-to csv:"Text - txt - csv (StarCalc)" # generate CSV spreadsheet output
--convert-to pptx:"Impress MS PowerPoint 2007 XML" # generate PPTX
--convert-to ppt:"MS PowerPoint 97" # generate PPT
--convert-to wmf:impress_wmf_Export # force "Impress" to generate the WMF
--convert-to wmf:draw_wmf_Export # force "Draw" to generate the WMF
--convert-to emf:impress_emf_Export # force "Impress" to generate the EMF
--convert-to emf:draw_emf_Export # force "Draw" to generate the EMF
--convert-to svg:impress_svg_Export # force "Impress" to generate the SVG
--convert-to svg:draw_svg_Export # force "Draw" to generate the SVG
--convert-to xlsx:"Calc MS Excel 2007 XML" # generate XLSX
--convert-to xls:"MS Excel 97" # generate XLS like Excel 97
--convert-to xls:"MS Excel 95" # generate XLS like Excel 95
--convert-to xls:"MS Excel 5.0/95" # generate XLS like Excel 5.0/95
Update
The current Version of libreoffice is working: v5.2.6.2 (mac)
I'm using the following command
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir . the_file_to_convert
OLD
It seems that the conversion is not working in newer version (> 4.2.4.2)
I'm using libreoffice 4.2.4.2
You can download older versions from http://downloadarchive.documentfoundation.org/libreoffice/old/4.2.4.2/
linux (using the deb package):
libreoffice4.2 --headless --convert-to pdf myfile.odt
windows:
swriter.exe --headless --convert-to pdf myfile.odt
Might sound stupid but I suffered from just this: In Ubuntu 14.04 it is not enough to install only the package libreoffice-core or -common to convert to PDF, but you need to install libreoffice-writer instead/additionally. The important thing to note is that libreoffice-core|common seems to bring in some functional libreoffice base, hence the name ;-). That doesn't error out if executed to convert some file, but instead just seems to wait on some socket or such for input or whatever... After I installed libreoffice-writer, which has [...]-core|common as a dependency, converting some ODT file to PDF worked like a charm with the exact same command line like used before.
Seems a bit crazy that the application looks like it works somehow, but simply couldn't ever because of missing packages, and doesn't tell anything about it.
I'm using LibreOffice 4.2.8.2 too (on GNU/Linux) but I tried to convert a DOC file to a HTML file. I've installed libreoffice-headless, but the terminal was hanging too. I've tried each bullet of Kurt Pfeifle, but it was not working…
The solution was simple: installing libreoffice-writer (maybe libreoffice-filters is ok too, but it depends on libreoffice-writer plus the other big components of LibreOffice).
Hope this will help.
I had the same problem and got stuck for an entire day; silly me ended by figuring out that I was trying to convert from and to a network file system; even tho I have written right libreoffice seems to not support it, or at least not in my configuration.
I solved this by converting to /tmp then moving the file by hand. Hope it helps.
I encountered problems converting HTML to xlsx using libreoffice. Other convert-to options worked fine. Turns out it needed the --calc flag additionally. In case anyone else runs into the same trouble. Tested on LibreOffice-6.4.3.2.
Credits to: Libreoffice converting html to xls or xlsx

Restore Batchfile, VB script shortcut in new System XP

In my old system (XP) I created many shortcuts in a folder on my desktop to open programs like eclipse, notepad++, etc. and some script files. I assigned shortcuts like
Cnrl+Alt+E for Eclipse, Cnrl+Alt+N for notepad, etc.
I have backed up and restored the shortcuts folder on the desktop and batch/script file folder to my new system in the same path.
Is there any script to register all the shortcut to registry in one go?
The hotkey is a function of the file.
XP looks in 4 places to discover hotkeys:
%UserProfile%\desktop
%AllUsersProfile%\desktop
%UserProfile%\Start Menu
%AllUsersProfile%\Start Menu
and in these three file types:
*.lnk
*.pif
*.url
as long as the files are in those locations, the hotkeys should still work
This vbs script will show you the hotkey assigned to a file:
on error resume next
set WshShell = WScript.CreateObject("WScript.Shell" )
Set Arg=Wscript.Arguments
set lnk = WshShell.CreateShortcut(Arg)
If lnk.hotkey <> "" then
msgbox Arg & vbcrlf & lnk.hotkey
End If
use it with the filename passed to the script, e.g. thescript.vbs %UserProfile%\desktop\myshortcut.lnk