Let's say that I want to press the "c" key for 5 seconds and then release it. That's what I'm trying to do:
tell application "System Events"
key down "c"
delay 5
key up "c"
end tell
But it just doesn't work. It always presses "a" regardless to the letter. I even tried with the key code 8 (equivalent for c) but it doesn't work either.
You should use the command keystroke. So the code would look something like this:
tell application "System Events"
set fiveSecondsFromNow to (time of (current date)) + 5
repeat until (time of (current date)) is fiveSecondsFromNow
keystroke "c"
end repeat
end tell
Related
Is there an application type in AppleScript?
I have this handler:
on doHandler(theApplication)
set theApp to ("\"" & theApplication & "\"")
tell application theApp
set frontWindow to theApp's (window 1)
etc.
end tell
end doHandler
It is accessed as follows:
doHandler("TextEdit")
This produces the obvious error on theApp's (window 1).
So, what is the correct call?
This cannot work. The argument of tell application must be a literal string because the terminology is evaluated at compile time.
Apart from that the code doesn't work anyway because it expects that every application has an AppleScript dictionary containing a window property which is not the case.
All you need to do is eliminate the line set theApp to... and use the its keyword to set up the correct reference.
doHandler("TextEdit")
on doHandler(theApplication)
tell application theApplication
set frontWindow to its (window 1)
end tell
end doHandler
In the main script, application references are established at compile time, so you can't have a variable app name, but handlers aren't evaluated until run time.
thanks in advance for your help.
New to Applescript.
Trying to open in Preview all files in folder and save them.
The problem is that saving pops up a dialog half the time, which requires me to sit there hitting enter.
Code:
tell application "Finder"
set fl to files of folder POSIX file "/Users/myname/Desktop/myfolder/" as alias list
end tell
repeat with f in fl
tell application "Preview"
activate
open f
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
save front document
close front document
end tell
end repeat
Thank you for the help
I made several PDF files and download from different sites and I get sample of version 1.3, 1.4, 1.5, 1.7, ..but no 1.6 ! For all of them no issue.
Anyway, because I have not been able to reproduce what you have, I took different approach.
1) I have added at top of the script, a list of encoding/version list which may required special treatment (like hit return key). You can of course amend these 2 lists to manage other cases you may have.
2) I changed your script to allow the script to get encoding and version values of the pdf. This is done by using spotlight data base via shell command 'mdls'. I use it 2 times to get version and encoding characteristics. The shell command returns characters before and after the value we want to get, so I use text x thru Y to extract the encoding and the version itself.
3) if PDF version/encoding are in the list predefined which requires special treatment, then I set OKReturn to true.
4) Just after the save instruction, the script now test if OKReturn is true. then I ask the script to hit return key for you. you may have to adjust this part, for instance, it could be not only 1 return, but 2 or something else. this is something I have not been able to test, because all my pdf are working. please keep in mind that because I simulate return key, you should not use the keyboard during the script run.
based on my test, I don't think the encoding is the blocking criteria. I think the version 1.6 is.Here is the script. It includes comment to make you able to adjust it:
set CodingReturn to {"Mac OS X 10.7.3 Quartz PDFContext"}
set VersionReturn to {"1.6"}
set myFolder to choose folder
tell application "Finder"
set fl to files of myFolder as alias list
end tell
repeat with f in fl
set FVersion to do shell script "mdls -name kMDItemVersion " & quoted form of POSIX path of f
set FEncoding to do shell script "mdls -name kMDItemEncodingApplications " & quoted form of POSIX path of f
if (length of FVersion) > 21 then set FVersion to text 19 thru -2 of FVersion -- extract only version number
if (length of FEncoding) > 42 then set FEncoding to text 38 thru -4 of FEncoding -- extract only the coding
set OKReturn to ((FVersion is in VersionReturn) and (FEncoding is in CodingReturn)) -- special treatment true/false
tell application "Preview"
activate
open f
repeat until (count of windows) > 0
delay 0.3
end repeat
save front document
if OKReturn then -- we need special key to be pressed
tell application "System Events" to keystroke return
end if
close front document
end tell
end repeat
I would be very interested to get your feedback about this version.
To save without the "save as" dialog popping up, add "in f". Here "f" was the filename. You could also put in a different path.
save front document in f
I wrote a script that would identify duplicates in iTunes, find their instances in playlists, and other things which I don't think are relevant. In order to check if a track is a duplicate, I have the following code:
set duplicateSongs to ¬
get (every track of playlist 1 where ¬
(name is equal to (name of currentTrackToCheck as text) and ¬
database ID is not equal to (database ID of currentTrackToCheck as integer) and ¬
time is equal to (time of currentTrackToCheck as text)))
I wrote and tested this against a test library with 1,000 songs and it was speedy. When I tested it against my iTunes library which is about 30,000 songs, then all hell broke loose.
That snippet of code takes 2 minutes to finish processing! Is there any way to make this faster code-wise? I've been reading a lot on AppleScript and I believe using where/whose is the fastest way to filter down results from a query.
Thanks! :-)
It won't affect speed, but I would write it like this...
tell application "iTunes"
set {trackName, trackDatabaseID, trackTime} to {name, database ID, time} of current track
set duplicateSongs to (get every track of playlist 1 whose name = trackName and time = trackTime and database ID ≠ trackDatabaseID)
end tell
Just changed to using the search function and then doing a search within that and it takes less than a second - yay! :-)
on SearchForDuplicates(currentTrackToCheck, iTunesLibrary)
tell application "iTunes"
set duplicateSongs to {}
set preSearch to search iTunesLibrary for (name of currentTrackToCheck as text) only songs
repeat with aTrack in preSearch
tell aTrack
if (name is equal to (name of currentTrackToCheck as text) and ¬
database ID is not equal to (database ID of currentTrackToCheck as integer) and ¬
time is equal to (time of currentTrackToCheck as text)) then
set end of duplicateSongs to aTrack
end if
end tell
end repeat
return duplicateSongs
end tell
end SearchForDuplicates
Usually i quickly update my TODO list creating a new empty file named like this:
2013-10-01 Tell a friend that stackoverflow rocks
2013-10-23 Prepare my super meeting about coding
and so on..
i just need a workflow or applescript that take all file in the folder, extract the date and the title from the file name and creates a new iCal event on that day with that title!
it seems so easy, but how can i achieve that?
Here's something in straight Applescript.
Note: It depends on you changing your date format to DD-MM-YYYY (due to Applescripts in built date parser)
tell application "Finder"
set data_folder to folder POSIX file "/Users/me/Desktop/my_ical_data"
set all_items to every item of data_folder
end tell
set my text item delimiters to {" "}
repeat with cur_item in all_items
set nm to the name of cur_item
set event_date to date (text item 1 of nm)
set event_desc to (text items 2 thru -1 of nm) as string
tell application "iCal"
tell calendar "Work" -- name of calendar you wish to add to
make new event with properties {summary:event_desc, start date:event_date, description:""}
end tell
end tell
end repeat
This does not require you to change the date format in System Preferences:
tell application "Finder" to name of items of folder POSIX file "/Users/username/todo"
repeat with l in result
set s to text ((offset of space in l) + 1) thru -1 of l
set d to current date
tell d to set {year, month, date, time} to {text 1 thru 4 of s, text 6 thru 7 of s, text 9 thru 10 of s, 0}
tell application "iCal" to tell calendar "Work"
make new event with properties {summary:s, start date:d}
end tell
end repeat
I am trying to create a service in OSX leopard that counts the number of words of selected text. I have automator set to run an applescript, with the following put in it:
on run {input, parameters}
count words of input
display alert "Words: " & input
return input
end run
When I compile the script, it says it cannot count every word. What am I doing wrong?
Thanks for the help,
Elliott
First of all, I presume you are testing this in Automator, and that's where the error is taking place? If so, the likely problem is that there is no input—so it can't count the words of nothing. In order to test it successfully, you need to temporarily add a "Get Specified Text" action before the Run AppleScript action, and enter some test text into that field. You'll have to remove the Get Specified Text action before using it as an actual service.
Secondly, you need to use
count words of (input as string)
in order to get a proper count, otherwise it'll return zero.
I made one here, on Github:
https://gist.github.com/1616556
The current source is:
on run {input, parameters}
tell application "System Events"
set _appname to name of first process whose frontmost is true
end tell
set word_count to count words of (input as string)
set character_count to count characters of (input as string)
tell application _appname
display alert "" & word_count & " words, " & character_count & " characters"
end tell
return input
end run
Use Automator.app to create a new service, and then select the Run AppleScript action. Paste this code in to the text box, and save as Word and Character Count. Now switch to a new app, select some text, and open the context menu to find the new option.