Previous  |  Main  |  Next
AppleScript Logo

Save Selected Text as XTags

Script for: QuarkXPress
Copyright: Matthias Steffens, use at your own risk!
Description: Exports the currently selected text / text box
as XTags into a text file.

For options on specifying the xtags file location
see top of script section.
Necessary: None
Download: SaveAsXTags_v1.0.hqx
The Script:
-- Alter the value of the 'XTagsFileLocation' property according to your needs:
--     "ask"        -> you will be prompted for a file location by use of a standard 'save file' dialog
--     "original"  -> the XTags file will be created within the same folder as your current document (acts the same as "ask" if doc wasn't saved yet)
--     "desktop"  -> the XTags file will be created on your desktop

property XTagsFileLocation : "original" -- other valid values are: "ask", "desktop"

tell application "QuarkXPress Passport™ 4.1"
	if exists (document 1) then
		tell document 1
			set XTagsFilePath to my GetXtagsFileLocation(XTagsFileLocation)
			
			if exists selection then -- get the selected text (or the selected text box)
				-- if (box type of current box) = text box then -- QuarkXPress 3.3
				if (box type of current box) = text box type then -- QuarkXPress 4.1
					try
						set SelectedText to (a reference to every text of selection)
						if SelectedText = "" then -- cursor is positioned within a text box but no text is selected
							my GetHelp()
						else
							my SaveAsXTags(SelectedText, XTagsFilePath)
						end if
					on error errText number errNum -- something went wrong... ?:-/
						if errNum is not -128 then -- '-128' = user canceled
							display dialog errText & return & return & ¬
								"(Error No: " & errNum & ")" buttons {"OK"} default button 1 with icon note
						end if
					end try
				else
					my GetHelp()
				end if
			else
				my GetHelp()
			end if
		end tell
	else
		display dialog "A document must be open in order to use this script." with icon note buttons {"Help", "OK"} default button 2
		if button returned of result is "Help" then my GetHelp()
	end if
end tell

on GetXtagsFileLocation(XTagsFileLocation)
	tell application "QuarkXPress Passport™ 4.1"
		tell document 1
			set XPressDocPath to file path -- get the file path of the front document
			if XPressDocPath is null then -- front document wasn't saved yet
				set XPressDocName to "Untitled Xtags File.xtg"
				set XTagsFileLocation to "ask"
			else
				set {XPressDocContainer, XPressDocName} to my SplitFilePath(XPressDocPath as string)
			end if
		end tell
	end tell
	
	if XTagsFileLocation = "desktop" then
		set XTagsFilePath to ((path to desktop as string) & XPressDocName) as string
	else if XTagsFileLocation = "original" then
		set XTagsFilePath to XPressDocContainer & XPressDocName
	else -- "ask" -> prompt the user for the XTags file location:
		set XTagsFilePath to (choose file name with prompt "Save Xtags file to:" default name XPressDocName) as string
	end if
	
	return XTagsFilePath
end GetXtagsFileLocation

on SplitFilePath(XPressDocPath) -- split the file path into path of container window & file name
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set XPressDocPathList to every text item of XPressDocPath
	set XPressDocContainer to (items 1 thru -2 of XPressDocPathList as string) & ":"
	set XPressDocName to item -1 of XPressDocPathList as string
	set AppleScript's text item delimiters to oldDelims
	-- strip any existing file name extension and append the correct one:
	if XPressDocName ends with ".qxd" then
		set XPressDocName to (characters 1 thru -5 of XPressDocName as string) & ".xtg"
	else
		set XPressDocName to XPressDocName & ".xtg"
	end if
	
	return {XPressDocContainer, XPressDocName}
end SplitFilePath

on SaveAsXTags(SelectedText, XTagsFilePath) -- finally, do the actual work:
	tell application "QuarkXPress Passport™ 4.1"
		tell document 1
			save SelectedText in XTagsFilePath as "TEXT" -- save as xtags file
		end tell
	end tell
end SaveAsXTags

on GetHelp() -- inform the user what's it all about :-)
	display dialog "This script exports the current text selection (or the characters contained in a selected textbox) as XTags to a text file." & ¬
		return & return & "I.e., in order to use this script you have to either select some text " & ¬
		"or a single textbox." buttons "OK" default button 1 with icon note
end GetHelp

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 29-Jan-01