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
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 am trying to clear out a shell script file if it exists but I keep running into the errors.
tell application "System Events"
set fileList to POSIX path of disk items of mdFolder
end tell
-->check to see if there is a shell script in the destination folder
repeat with i from 1 to count of every item in fileList
set thisFile to item i of fileList
set oasd to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set fileName to last text item of thisFile
if fileName is equal to "ditaShell.sh" then
tell application "System Events" to delete thisFile
end if
end repeat
tell application "Finder"
-->create new file for shell script
make new file at mdFolder with properties {name:"ditaShell.sh"}
display dialog "Created new file"
end tell
I get this error message:
System Events got an error: Can’t make "/Users/xxxxxxxxx/Desktop/DITAConversions/Input/ditaShell.sh" into type disk item.
I solved it, was overthinking the problem:
tell application "Finder"
delete (every file of folder mdFolder whose name is "ditaShell.sh")
-->create new file for shell script
make new file at mdFolder with properties {name:"ditaShell.sh"}
display dialog "Created new file"
end tell
could anyone tell me how I can create a simple installer of Kexts using AppleScrit in Xcode?
I've tried the following:
on installKext:sender
choose file
copy the result to thisFile
copy the (info for thisFile) to fileInfo
copy the name extension of the fileInfo to nameExtension
copy the file type of the fileInfo to filetype
tell application "Finder"
if the filetype is "Kext" or ¬
the nameExtension is "Kext" then
move thisFile to folder "Extensions" of folder "Library" of folder "System" of startup disk
end if
end tell
end installKext:
I Have this errors:
installKext:]: Not authorized to send Apple events to Finder. (error
-1743)
Error Domain=PlugInKit Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
You cannot move files with the Finder into folders which belong to the system.
You need root privileges for example with do shell script .... with administrator privileges
on installKext:sender
set theFile to choose file
tell application "System Events"
set {name extension:nameExtension, file type:fileType} to theFile
end tell
if fileType is "Kext" or nameExtension is "Kext" then
do shell script "mv " & quoted form of POSIX path of theFile & space & "/System/Library/Extensions/" with administrator privileges
end if
end installKext:
You must add the following in info.plist:
now everything works like a charm.
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 an applescript that looks like this:
repeat
tell application "Adobe Reader"
open "filepath/name.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name1.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name2.pdf"
end tell
delay (60)
end repeat
I want to be able to close pdf windows after they have been opened. The issue is that these pdfs reside on a share and users have the ability to update them. The script will only display the updated pdf if it is stopped and restarted. I do not want to have to do this manually. How can I do this?
Here's a solution, which makes Preview scriptable and then continues to open and close a file of your choice.
-- http://www.macworld.com/article/1053391/previewscript.html
on addAppleScriptFeatures()
try
tell application "Finder"
set the Preview_app to (application file id "com.apple.Preview") as alias
end tell
set the plist_filepath to the quoted form of ¬
((POSIX path of the Preview_app) & "Contents/Info")
do shell script "defaults write " & the plist_filepath & space ¬
& "NSAppleScriptEnabled -bool YES" with administrator privileges
do shell script "chmod -R 755 " & the quoted form of (POSIX path of the Preview_app) with administrator privileges
return true
on error myErr number MyNr
display dialog myErr & " (" & MyNr & ")." buttons {"OK"} default button "OK" with icon 0
return false
end try
end addAppleScriptFeatures
if addAppleScriptFeatures() then
set f to choose file
tell application "Preview"
activate
open f
delay 5 -- short for testing
close window 2
end tell
end if