I’m trying to write the Clipboard text to an existing file named myCompactLog.txt in a folder named MyJournals on the Desktop.
When I use any other file name except myCompactLog.txt, the script below works fine. The named file is created if it doesn’t exist, and the Clipboard is inserted at the top of any existing text. However when I try to name it myCompactLog.txt, nothing gets written to the file, whether newly created or already existing. No errors, but no text.
In the following code, several alternative file names are included to try out. They all work except for the first one. What’s wrong with that for a file name?
(*
Prepend the contents of the Clipboard to an existing text file named myCompactLog.txt in a folder named MyJournals on the Desktop
Add a blank line and a date stamp before each entry
*)
set fileName to "myCompactLog.txt"
--set fileName to "myCompacLog.txt"
--set fileName to "myCompact_Log.txt"
--set fileName to "myCompactLogo.txt"
--set fileName to "Log.txt"
set desktopPath to (path to desktop) as string
set folderName to "MyJournals"
set folderPath to (desktopPath & folderName & ":")
set dateStamp to "------- Clipped on " & (do shell script "date +%Y+%m+%d+%H+%M+%S") & " -------"
tell application "Finder"
if not (exists folder folderPath) then
try
make new folder at desktop with properties {name:folderName}
set folderPath to the result as alias
on error
return "Can't make new folder named " & folderName
end try
end if
set filePath to (folderPath as string) & fileName
if not (exists filePath) then
try
make new file at folderPath with properties {name:fileName}
set filePath to the result as alias
on error
return "Can't make new file named " & fileName
end try
end if
end tell
set filePath to filePath as string
try
close access filePath
end try
try
set N to open for access file filePath with write permission
get eof N
if result > 0 then
set oldText to read N
set eof N to 0
else
set oldText to ""
end if
write return & dateStamp & return & (the clipboard as text) & return & oldText & return to N
close access N
on error
try
close access N
return "Can't write to file " & fileName
end try
end try
return true
An error occurs when the clipboard is empty.
Get the clipboard in a separate try block, so you can set a message when it’s empty.
edit:
On 2nd thought, you have way too many try blocks in this simple script. Remove them all, so you get to see the actual errors. Elemental: first get the script working, then put in error management for foreseeable errors.
Thanks guys, but it’s still not working for me. Adding “file” before filePath didn’t change anything.
The following refers to these lines of code, so I’m not repeating the whole script each time.
--earlier code . . . . . . .
set filePath to filePath as string
try¨
close access file filePath # EDITED¨
end try
try
set N to open for access file filePath with write permission
--later code . . . . . . .
First attempt: remove try blocks
At the entry to this snippet, the correct file exists at the end of filePath, and is not open for access.
set filePath to filePath as string
--try¨
close access file filePath # EDITED <-- Script Error: File file Mac-5K:Users:roy:Desktop:MyJournals:myCompactLog.txt wasn't open.
¨--end try
--try
set N to open for access file filePath with write permission
Second attempt: jump over the first error
set filePath to filePath as string
--try
¨--close access file filePath # EDITED
--end try
--try
set N to open for access file filePath with write permission <-- Script Error: File file Mac-5K:Users:roy:Desktop:MyJournals:myCompactLog.txt is already open.
So AS is confused about whether or not the file is open for access. It isn’t clear why this specific file name has an effect like this. I guess the simplest answer is just to use a different name that works, but I’d really like to understand what’s happening.
I repeat that on my machine, after editing as I wrote, the script behaved perfectly.
The events log was :
tell current application
path to desktop
do shell script "date +%Y+%m+%d+%H+%M+%S"
end tell
tell application "Finder"
exists folder "SSD 500:Users:userName:Desktop:MyJournals:"
exists "SSD 500:Users:userName:Desktop:MyJournals:myCompactLog.txt"
end tell
tell current application
close access file "SSD 500:Users:userName:Desktop:MyJournals:myCompactLog.txt"
open for access file "SSD 500:Users:userName:Desktop:MyJournals:myCompactLog.txt" with write permission
get eof 326
read 326
set eof 326 to 0
end tell
tell application "Script Editor"
the clipboard as text
end tell
tell current application
write "
------- Clipped on 2016+03+30+21+54+10 -------
tell application \"Script Editor\"
the clipboard as text
end tell
------- Clipped on 2016+03+30+21+53+00 -------
''
------- Clipped on 2016+03+30+12+22+15 -------
use scripting additions
use framework \"Foundation\"
use script \"BridgePlus\"
load framework
set nameToSearch to \"read the Dock.scpt\"
set theResult to current application's SMSForder's runSpotlightQuery:\"kMDItemFSName CONTAINS %@\" queryValues:{nameToSearch} inFolders:{(path to desktop)} |error|:(missing value)
--ASify from theResult
--> {\"/Users/userName/Desktop/read the Dock.scpt\"}
set posixFilesFound to theResult as list
--> {\"/Users/userName/Desktop/read the Dock.scpt\"}
if posixFilesFound is {} then
error \"Don't find a file named "\" & nameToSearch & \"".\"
end if
set theFile to POSIX file (posixFilesFound's item 1)
tell application \"Finder\"
open theFile
end tell
" to 326
close access 326
end tell
Thanks, Yvan. The problem turned out to be the running version of ScriptEditor had become corrupted, and it was locked into thinking that the file was open, even though it wasn’t. Restarting ScriptEditor cured the problem. Of course I should have done that to start with, but everything else seemed to be working perfectly. Lesson learned.
I was doing 2 versions (now finished and working). So in the version above, the text is added in front of existing text, and the other version puts it at the bottom.
There is one other thing related to the above that I could use your help on. In finding a reference to a file, I seem to be using a lot of code to do something that I’ve seen you do in 3-4 lines.
So, what’s the best (or at least, better) way to get a filePath to a file (fileName) in a folder (folderName) on the Desktop, if they exist, or if not then create them?
When I must write datas in a text file, I use this good old handler :
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
open for access is fair enough to create by itself the file if it doesn’t already exists.
In your code, you have :
tell application "Finder"
if not (exists folder folderPath) then
try
make new folder at desktop with properties {name:folderName}
set folderPath to the result as alias
on error
return "Can't make new folder named " & folderName
end try
end if
set filePath to (folderPath as string) & fileName
if not (exists filePath) then
try
make new file at folderPath with properties {name:fileName}
set filePath to the result as alias
on error
return "Can't make new file named " & fileName
end try
end if
end tell
set filePath to filePath as string
try
close access filePath
end try
which may be shortened as :
tell application "Finder"
if not (exists folder folderPath) then
try
make new folder at desktop with properties {name:folderName}
--set folderPath to the result as alias
on error
return "Can't make new folder named " & folderName
end try
end if
end tell
set filePath to folderPath & fileName
try
close access filePath
end try
or, as I hate the Finder,
tell application "System Events"
if not (exists folder folderPath) then
try
make new folder at folder desktopPath with properties {name:folderName}
--set folderPath to the result as alias
on error
return "Can't make new folder named " & folderName
end try
end if
end tell
set filePath to folderPath & fileName
try
close access filePath
end try
If filePath doesn’t exist, close access filepath would fail but the error would be intercepted by try / end try.
after that, you will enter the code using open for access which will create the file if necessary.
Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 1 avril 2016 11:47:29
Thanks, Yvan - That clears up a lot that was murky.
One question about dataTypes. From the handler:
– dataType is the data type of theData and it can be text, list, record etc.
What’s “etc”? What other dataTypes would also work in this handler? (I’m looking at http://macscripter.net/viewtopic.php?id=24695)
Could you give an example of what theData might look like when dataType is record?