I’ve been searching the forums and the web without coming up for an answer. I am trying to create a script that writes the contents of the OS X clipboard to a file and then converts the file to an MS-DOS text file that can be opened by ancient DOS software (specifically, WordPerfect for DOS). What I’ve got, in summary, looks like this (I realize that this is a terrible mess, but I’ve been trying to get it at least working before making it more efficient)
set outputFolder to SOME EXISTING FOLDER
set f to ((outputFolder) & "MacClip.txt")
set newClip to the clipboard as Unicode text
writeClip(f, newClip)
on writeClip(f, newClip)
set newClip to «class ktxt» of (newClip as Unicode text as record)
set fPos to POSIX path of f
set f to (open for access file f with write permission)
write newClip to f
close access f
set outputAlias to outputFolder as alias
set outputPOSIX to POSIX path of outputAlias
set hPos to outputPOSIX & "CLIP.TXT"
try
do shell script "iconv -f UTF-16 -t 437 " & fPos & " > " & hPos
on error e
display dialog "iconv error: " & e
end try
end writeClip
I’ve tried also UTF-8 and UTF-32 as the “from” parameter; but it invariably throws an error message “Cannot convert”.
I can use iconv correctly in other scripts. What am I doing wrong here? And is there a simply way to write the clipboard to an MS-DOS text file with code page 437?
Thanks for any help.
I see several possible things that would cause problems:
- the subroutine “writeClip” does not know the variable “outputFolder”. That will error.
- file paths should be “quoted” because if they contain spaces then the shell will misinterpret them. Use “quoted form of” to have applescript handle this for you.
- another problem could be when you open access on the file. You set f, which is the variable holding the file path, to the open file variable. I would use a different variable.
I don’t know the “iconv” shell command, but assuming your command is correct here’s how I would write your script. Note that I’m getting errors with the iconv command so I’m not sure if your command is correct. I am sure the applescript part is correct.
set outputFolder to path to desktop as text
set f to outputFolder & "MacClip.txt"
set h to outputFolder & "CLIP.TXT"
set newClip to the clipboard as Unicode text
writeClip(f, h, newClip)
on writeClip(f, h, newClip)
set openFile to open for access file f with write permission
write newClip to openFile
close access openFile
try
do shell script "/usr/bin/iconv -f UTF-16 -t 437 " & quoted form of POSIX path of f & " > " & quoted form of POSIX path of h
on error e
display dialog "iconv error: " & e
end try
end writeClip
Hi,
this simplyfied version seems to convert the Cilp into DOS Latin 1:
set thefile to (path to desktop) & "test.txt" as text
set Clipfile to (path to desktop) & "Clip.txt" as text
set newClip to the clipboard as Unicode text
set openFile to open for access file thefile with write permission
set eof of the openFile to 0
write newClip to openFile starting at eof
close access openFile
do shell script "iconv -f UTF-16 -t 437 " & quoted form of POSIX path of thefile & " > " & quoted form of POSIX path of Clipfile
Hans-Gerd,
That is perfect; thank you! I also found that my original code would work if I replaced UTF-16 with MAC, but yours is much more elegant. Thank you again.
Regulus6333,
You are of course right about the variable not being passed in the code I posted. That’s because I showed only a fragment of the code, and neglected to note that I was using a global variable for outputFolder, because I use that in other routines. Your code helped me see how to clean up mine - thank you again.
Thank you both - and now I have another question about the clipboard, but I’ll start another thread for that one.