Tryng to save a text file with the 2nd line of a txt document
Textwrngler allows me to select the second line as such
tell application "TextWrangler"
activate
make new text document with properties {contents:current clipboard}
select insertion point before line 2 of document 1
copy selection
end tell
tell application "Finder"
set name of document 1 to (the clipboard)
end tell
tell application "TextWrangler"
activate
save
end tell
tell application "TextWrangler"
activate
close project window 1 saving no
end tell
However the Finder doesn’t save using the clipboard content
How to solve this problem
Thanks
I got several errors with your script.
(1) copy selection grabbed nothing so the file name was not defined
(2) Finder is unable to set the name of a TextWrangler document.
(3) As far as I know, to save a document, a name is not sufficient, we need a full pathname.
Given that I rebuilt your script as :
set destinationFolder to choose folder with prompt "Choose the destination folder" # ADDED
tell application "TextWrangler"
activate
set newDoc to make new text document with properties {contents:current clipboard}
(*
select insertion point before line 2 of document 1
copy selection --> Erreur dans TextWrangler : The selection range must be non-empty before attempting a cut or copy.
*)
set theText to text of newDoc
end tell
set theName to paragraph 2 of theText
# No longer assuming that you want to save in the Documents folder
set newPath to (destinationFolder as text) & theName
close access (open for access newPath)
tell application "TextWrangler"
activate
save newDoc to file newPath
close project window 1 saving no
end tell
You may use a more compact code :
set destinationFolder to choose folder with prompt "Choose the destination folder" # ADDED
set theText to the clipboard
set theName to paragraph 2 of theText
# Assuming that you want to save in the Documents folder
set newPath to (destinationFolder as text) & theName # EDITED
tell application "TextWrangler"
activate
set newDoc to make new text document with properties {contents:theText}
(*
select insertion point before line 2 of document 1
copy selection --> Erreur dans TextWrangler : The selection range must be non-empty before attempting a cut or copy.
*)
tell me to close access (open for access newPath)
save newDoc to file newPath
close project window 1 saving no
end tell
In both case, I wonder if the paragraph 2 contains a complete filename (with its file extension) or if it contains only the bare name.
In my tests the paragraph contained “activate” which - at least from my point of view - is not a correct value for a file name. “activate.txt” would be better. The extension may be added by the script using :
set newPath to (path to documents folder as text) & theName & “.txt”
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mercredi 25 janvier 2017 21:00:45
Thanks Ivan it works perfectly
However can I add something like
DestinationFolder (Choose)
As the files are very many and the Document folder will become a mess
Thanks again
I see no need for using TextWrangler to do the job.
Plain old fashioned AppleScript may do the job easily :
set destinationFolder to choose folder with prompt "Choose the destination folder"
set theText to the clipboard
set theName to paragraph 2 of theText
set newPath to (destinationFolder as text) & theName as «class furl»
set newDoc to open for access newPath with write permission
write theText to newDoc as «class utf8»
close access newDoc
You may also use ASObjC :
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set destinationFolder to choose folder with prompt "Choose the destination folder"
set theText to the clipboard
set theName to paragraph 2 of theText
set newPath to POSIX path of (destinationFolder as text) & theName
set theString to current application's NSString's stringWithString:theText
set theResult to theString's writeToFile:newPath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 26 janvier 2017 11:40:12