Good evening all,
I am trying to understand why I can run the below script in Applescript Editor but am unable to successfully run it in an Xcode Cocoa-Applescript application.
Can someone please explain why this function will not run in Xcode and what I need to do to have it run correctly?
set this_data to ((current date) as string) & space & "WHY DOESN'T THIS LOG!!!!" & return
set this_file to (((path to desktop folder) as string) & "MY LOG FILE")
my write_to_file(this_data, this_file, true)
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
This is one of the Xcode behaviours I can't explain, but it works if you add tell current application like this:
tell current application to set the open_target_file to open for access file target_file with write permission
It might be better to put the whole function in a tell current application block.
Related
I am trying to create a script that can create a text file with random information, and then duplicate the file onto any computer's desktop. Therefore, I might need a way of reading a user's directory.
This AppleScript code works for me using the latest version of macOS Mojave.
property fileName : "Test Document.txt" --value can be changed
property theText : "Random Information" --value can be changed
writeToAFile()
on writeToAFile()
set theFile to (path to desktop as text) & fileName
set theFile to POSIX path of theFile
try
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
on error errMsg number errNum
close access theFile
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
end try
end writeToAFile
I'm trying to set up an Indesign script that copies the link information of a selected image and appends it to a text file to the desktop, if the text file does not exist it creates one. The link information is copied to the clipboard using a custom key command in inDesign. The problem is if I copy some text or an image beforehand, that text remains on the clipboard. Even though I copy new link info and set that clipboard to a variable.
I know this is an ugly script, I'm just good enough to get by, but barely.
Any suggestions on how to clear out the clipboard?
Thanks,
~David
--dialog box
display dialog "What do you need done" default answer "Remove Background"
set theAnswer to (text returned of result)
set this_story to "
------------------- " & theAnswer & " -------------------
"
set this_file to (((path to desktop folder) as string) & "Corrections.txt")
my write_to_file(this_story, this_file, false)
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is true then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
--copy link information using keyboard short
activate application "Adobe InDesign CC 2015"
tell application "System Events" to keystroke "l" using {shift down, control down}
end
---
set myVar to the clipboard
my write_clip_file(myVar, this_file, false)
on write_clip_file(myVar, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is true then set eof of the open_target_file to 0
write myVar to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_clip_file
Here is a cleaned up version of your script. You don't need to code the same handler twice... both times you right to the file, you can use the same handler. Also, I reversed the logic on the append_data variable, because you had the logic backwards. (if you DO append, then you DON'T set eof to 0 cause that erases the current content).
I don't know what function you're calling in InDesign, but sounds like it's placing the file path/link of a selected image onto the clipboard. When using the clipboard to get data and to place the data into a variable, it's a good practice to put some delays in the script, to avoid the script moving on to the next commands before the app you're working with finishes it's work. I'm betting that the problem you're having is that the script is proceeding before InDesign has finished copying to the clipboard. So, in this script, you can see I placed three delays surrounding the work with the clipboard. You can play with reducing the delay times to see what will still work reliably.
--dialog box
display dialog "What do you need done" default answer "Remove Background"
set theAnswer to (text returned of result)
set this_story to "
------------------- " & theAnswer & " -------------------
"
set this_file to (((path to desktop folder) as string) & "Corrections.txt")
my write_to_file(this_story, this_file, true)
--copy link information using keyboard short
tell application "Adobe InDesign CC 2015"
-- tell application "TextEdit" -- for testing purposes
activate
delay 0.9 -- give app time to come to the front before copying to clipboard
tell application "System Events" to keystroke "l" using {shift down, control down}
-- tell application "System Events" to keystroke "c" using {command down} -- for testing purposes
delay 0.9 -- wait til clipboard is copied before continuing with the script
end tell
set myVar to the clipboard
delay 0.1 -- wait til variable is pulled from clipboard before moving on
my write_to_file(myVar, this_file, true)
--
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
Try this out and tweak it as you need to, and then let me know if anything is still not working.
I have some applescript for creating a list of the fiels under iTunes control and writing it to a file, this is the complete script:
tell application "iTunes"
if (count of every file track of library playlist 1) is equal to 0 then
set thePath to (POSIX file "/tmp/songkong_itunes_model.txt")
set fileref to open for access (thePath) with write permission
set eof fileref to 0
return
end if
tell every file track of library playlist 1
script performancekludge
property tracknames : its name
property locs : its location
property persistids : its persistent ID
end script
end tell
end tell
set thePath to (POSIX file "/tmp/songkong_itunes_model.txt")
set fileref to open for access (thePath) with write permission
set eof fileref to 0
tell performancekludge
repeat with i from 1 to length of its tracknames
try
set nextline to item i of its tracknames ¬
& "::" & POSIX path of item i of its locs ¬
& "::" & item i of its persistids
write nextline & linefeed as «class utf8» to fileref
end try
end repeat
end tell
close access fileref
and sometimes it gives
create_itunes_model.scpt:428:436: execution error: iTunes got an error: AppleEvent timed out. (-1712)
for some users, but I dont know why
Does anybody know why, or how I could improve my script
First you should close access fileref before your first return. Otherwise the text file could stay in an opened state.
The error looks like a timeout if the user's iTunes Library is too big and the script takes too much time to run. You should use a with timeout-block:
with timeout of 600 seconds
-- your script here
end timeout
Have fun, Michael / Hamburg
am using the below script for writing the file, but sometimes am getting errors like mentioned below, pls suggest..
set filepath to POSIX path of "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
try
tell application "System Events"
if file filepath exists then
set myFile to open for access file filepath with write permission
set fileData to read myFile
set eof myFile to 0
write "blah blah" to myFile
close access myFile
else
return "File Not Found"
end if
end tell
on error
return false
end try
Error:
"Network file permission error." number -5000 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
Also some times i will get this error, am unable to close the opened file
"File file Macintosh HD:Library:Application Support:Macromedia:mms.cfg is already open." number -49 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
When i tried to close the ofile am getting this eror:
on openAFile(filepath)
try
set fp to open for access filepath with write permission
on error errstr number errNum
if errNum = -49 then
close access filepath
set fp to open for access filepath with write permission
else
display dialog errstr
return false
end if
end try
return fp
end openAFile
set pointer to openAFile("Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
set fileContents to read pointer
Error
"Can’t make \"Macintosh HD:Library:Application Support:Macromedia:mms.cfg\" into type file." number -1700 from "Macintosh HD:Library:Application Support:Macromedia:mms.cfg" to file
I cannot explain the "Network file permission error" that you are receiving.
The "File file Macintosh HD:Library:Application Support:Macromedia:mms.cfg is already open." error happens when you script stops in the Script Editor without closing the file. This happens if there is some other logic error that prevents AppleScript from reaching the close access command in your on error block (or you press the Stop button at an inopportune time). Apple's AppleScript Editor does not close leaked file references for you when a script stops.
Your workaround of trying to close the file before opening it may work, but you are passing a string rather than a file or alias for the filepath. Use open for access file filepath and close access file filepath.
I'm trying to write content to a file, /tmp/test.txt with AppleScript.
If the file doesn't exist, I want to create it.
If it does exist, I want to replace the contents.
This is proving quite difficult because AppleScript has a different syntax for creating a new file versus writing to an existing file. I also thought about deleting a file if it already exists, and then creating it, but AppleScript will move things to the trash by default and so even that is rather complicated.
The data I am going to write to the file may have a lot of special characters in it, so I don't really want to do something like do shell script "cat " + data + " > /tmp/txt.txt" because I am unaware of any escapeshellarg for AppleScript.
Below is what I have so far but I'm getting an error:
Can't make file "Macintosh HD:private:tmp:test.txt" into type reference.
I would really like to abstract this by improving the helper function write_to_file that I got here so that it would take care of creating the file if it does not exist.
my write_to_file("hello", "/tmp/test.txt", false)
on write_to_file(this_data, target_file, append_data)
try
if not (exists POSIX file target_file) then
make new document file at ("/tmp/" as POSIX file as alias) with properties {name:"test.txt", text:the_data}
end if
set the target_file to the target_file as POSIX file
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error e
try
display dialog e
close access file target_file
end try
return false
end try
end write_to_file
How can I create or replace a file using Applescript?
The first mistake you've made is mixing POSIX paths with HFS paths. A posix path is /tmp/test.txt and a hfs path to the same file will be Macintosh HD:tmp:test.txt if the name of the boot volume is Macintosh HD of course.
open for access file "Volume:Path:to:file.txt"
The second mistake is that for Mac OS X the /tmp folder is not the place to store temporary files. You will need administrator privileges to write there and the user will be prompted for username and password. It is stored in /var/folders and there is a folder created at login for each user, also with fast user switching. To access the path to this folder you should use the command path to temporary items. Temporary items will be removed between startups.
set theFile to (path to temporary items as string) & "test.txt"
The third mistake is that when you're using quoted form of you can use do shell script. Since AppleScript is unicode like the shell there is no such thing as that characters can't be handled by do shell script.
do shell script "/bin/echo -n " & (quoted form of "$#%^&*()#") & " > " POSIX path of theFile
At last you've made the mistake that you need to check for existence of a file. When you're using open for access command the file will be created when it doesn't exists for you. So the only code you need is:
set theFile to (path to temporary items as text) & "test.txt"
writeToFile(theFile, "Hello!", false)
writeToFile(theFile, (linefeed & "Goodbye!" as string), true)
on writeToFile(p, d, a)
try
set fd to open for access file p with write permission
if not a then set eof of fd to 0
write d to fd starting at (get eof of fd) as «class utf8»
close access fd
on error
close access file p
end try
end writeToFile
or using a do shell script which creates an UTF-8 file as well but the given path needs to be an POSIX path now
set theFile to (path to temporary items as text) & "test.txt"
writeToFile(POSIX path of theFile, "goodbye!", false)
writeToFile(POSIX path of theFile, (linefeed & "hello!" as string), true)
on writeToFile(p, d, a)
set cmd to "/bin/echo -n " & quoted form of d & " >"
if a then set cmd to cmd & ">"
do shell script cmd & space & quoted form of p
end writeToFile
Choose the one to your likings
Try:
my write_to_file("hello", "/tmp/test.txt", true)
on write_to_file(this_data, target_file, append_data)
try
tell application "System Events" to exists file target_file
if not the result then do shell script "> " & quoted form of target_file
set the open_target_file to open for access target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error e
try
display dialog e
close access target_file
end try
return false
end try
end write_to_file