Applescript: How can I make TextEdit be satisfied?

Discussion in 'Mac Programming' started by balamw, Dec 24, 2010.

  1. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #1
    I have a dumb AppleScript question (Applescript n00b here). Trying to modify Doug Adams' "Show Description" script http://dougscripts.com/416 to save the iTunes long description for an item to a text file instead of displaying it in a dialog.

    This works, but TextEdit prompts for confirmation at the close. I think it is because of the formatting info that may be lost when it was saved as text. Is there any way for me to avoid the confirmation dialog?

    Code:
    tell application "iTunes"
    	activate
    	if selection is not {} then
    		set sel to item 1 of selection
    		my save_info(sel)
    	end if
    end tell
    
    to save_info(sel)
    	with timeout of 3000 seconds
    		tell application "iTunes"
    			tell sel
    				set {desc, loc} to {long description, location}
    				if desc is missing value then set desc to ""
    				set ploc to the POSIX path of loc
    				set ldpath to ploc & ".txt"
    				(* display dialog ldpath *)
    				tell application "TextEdit"
    					activate
    					make new document at the front
    					set the name of the front document to ldpath
    					set the text of the front document to desc as text
    					save the front document in ldpath
    					close the front document
    				end tell
    			end tell
    		end tell
    	end timeout
    end save_info
    B
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    If the goal is to write desc as text to a file, maybe you can skip TextEdit altogether. Instead, use a simple command-line to echo desc as text with stdout redirected to the target file. I'm assuming you're familiar with command lines.

    There are also AppleScript verbs for reading and writing text files. Open the Standard Additions scripting extension (Script Editor, Open Dictionary, scroll down).
     
  3. Dalton63841 macrumors 65816

    Dalton63841

    Joined:
    Nov 27, 2010
    Location:
    SEMO, USA
    #3
    A simple workaround would be to add this:
    Code:
     tell application "System Events"
    		keystroke "T" using {command down, shift down}
    	end tell
    
    Put that between
    Code:
    make new document at the front
    and
    Code:
    set the name of the front document to ldpath
    That uses the keyboard shortcut CMD+SHFT+T to convert the new document to plain text, before any text is added to the document.
     
  4. balamw, Dec 24, 2010
    Last edited: Dec 24, 2010

    balamw thread starter Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    I am, but was concerned that the text block might be too long. It might work most of the time, but seems less robust than using TextEdit.

    I considered something like
    Code:
    do shell script "echo" & desc & " > " & ldpath
    , but in that form there is some limit to the length of desc the command line can handle and I'd have to be concerned about spaces in ldpath.

    EDIT: I checked and by lengths I should be fine. The long description is limited to 4000 characters which is much less than ARG_MAX, would still have to escape the target filename somehow.

    That does it perfectly, thanks!

    I was searching for some equivalent way to do that in the dictionary, I would have hoped that plain text would be a property of the document, but I guess sending keystrokes is fine.

    B
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Hence my further suggestion of the file read/write verbs in AppleScript.
    They are under the "File Read/Write" heading of StandardAdditions.osax.
    The relevant verbs are:
    open for access, write, and close access.

    AppleScript has a quoted form modifier for strings. Google search terms:
    applescript quoted form

    It should be applied to both the text (which may contain quotes, apostrophes, or spaces) as well as the file pathname. Or try the AppleScript verbs noted above.
     
  6. balamw, Dec 24, 2010
    Last edited: Dec 24, 2010

    balamw thread starter Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Now we're in familiar territory! Google revealed both the shell approach and TextEdit approach, but not that one which is what I was searching for. I've got some reading to do.

    EDIT: What I ended up with:

    Code:
    tell application "iTunes"
    	activate
    	if selection is not {} then
    		set sel to item 1 of selection
    		my save_info(sel)
    	end if
    end tell
    
    to save_info(sel)
    	with timeout of 3000 seconds
    		tell application "iTunes"
    			tell sel
    				set {desc, loc} to {long description, location}
    				set theFilePath to ((loc as string) & ".txt" as string)
    				set fid to open for access theFilePath with write permission
    				set eof of fid to 0
    				write desc to fid starting at eof
    				close access fid
    			end tell
    		end tell
    	end timeout
    end save_info
    
    B
     

Share This Page