Save as file in Default Location for any user

Hi Guys,

I am using Mac OS X (Mountain Lion) Version 10.8, Applescript Editor Version 2.5 and Microsoft Word for Mac 2011.

I have created the below script to prompt the user to select and cut the text and to paste in a new document and to saveas the file in text format.

Everything is fine. But the saveas file is saved in default location.

How to save it in default path i.e., Desktop/Test for all users (If the Test folder is not available in Desktop, It should be created.
And if the file already exist in that location, should be replaced with the new file.

tell application "Microsoft Word"
	activate
	set mychoice to (choose from list {"PS Bullet List", "PS Table", "PS Legend", "PO Chart Text", "MD", "PM Dis", "MS Text"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
	if mychoice is false then error number -128 -- user canceled
	
	display dialog "Select the text" buttons {"OK"}
	
	delay 5
	
	tell application "System Events"
		tell process "Microsoft Word"
			keystroke "x" using command down
			keystroke "n" using command down
			keystroke "v" using command down
			tell application "Microsoft Word"
				set currentDoc to active document
				save as currentDoc file format format text file name (mychoice & ".txt" as text)
				close currentDoc saving no
			end tell
		end tell
	end tell
end tell

Thanks,
John

Hi,

why GUI scripting ??
Word 2011 has a huge AppleScript dictionary


tell application "Finder"
	if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS Bullet List", "PS Table", "PS Legend", "PO Chart Text", "MD", "PM Dis", "MS Text"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled
display dialog "Select the text" buttons {"OK"}
tell application "Microsoft Word"
	set theContent to content of text object of selection
	set newDoc to make new document
	insert text theContent at end of text object of newDoc
	save as newDoc file format format text file name (desktopTestFolder & mychoice & ".txt")
	close document 1 saving no
end tell

Hi StefanK,

Thanks for your immediate response. It works Charm. I am new to scripting world and I not familiar with Application Dictionaries. So that only gone through GUI Scripting.

Thanks,
John

Hi Stefank,

Littlebit help needed.

Today only I have involved in full testing. The script copy the selection and paste the text into the new document charmly. But one thing I have noted is, the script pasting the text as unformatted text (all format has been removed). Before saving the new doc as .txt file I have to run some simple macros on the Formatted text i.e., (bold, italic, bolditalic, superior etc)

I googled and got the below script from (http://word.mvps.org/mac/scripts/pastetextas.html). It pasted bold and italic text. But I am not sure what it exactly do.

Paste Text Matching Destination Format

tell application "Microsoft Word"
paste and format (text object of selection) type format surrounding formatting with emphasis
activate
end tell

instead of using

insert text theContent at end of text object of newDoc

can I use the above to paste the selection? What are the difference between these two?

Thanks,
John

as you are going to save the document as plain text (.txt) the formatting will be ignored anyway

Hi Stefank,

paste and format (text object of selection) type format surrounding formatting with emphasis

I think this code is pasting the copied text into the new doc.

Whereas yours code is pasting the selected text.

What is the code to copy or delete the Selection.

Thanks,
John

it doesn’t matter, because the new document is saved in format text (plain text without formatting)

to copy and paste text with formatting in Word, you can use this


.
copy object text object of selection
	set newDoc to make new document
	paste object text object of newDoc
.

Thanks a Lot