PDA

View Full Version : Applescripting stickies application?




VideoBeagle
May 29, 2012, 08:04 PM
I have a folder action that will read a text file i place in a drop box folder, and make an alert message on my screen.

I'd prefer for it to make a new stickies note.

I've not found any applescriptness about stickies yet, though.

Anyone know if what I want to do is possible?



kryten2
May 29, 2012, 10:03 PM
It's not clear to me what exactly you're trying to achieve. A new Stickies note with the contents of the text file or with the text from the alert message or just a new Stickies note?

A new Stickies note :

tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 0.5
keystroke "n" using command down
end tell
end tell
end tell

A new Stickies note with contents of a text file :

set the_file to read alias "Macintosh HD:path:to:textfile.txt" --(choose file)
set the clipboard to the_file
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
--delay 0.5
--keystroke "n" using command down
keystroke "v" using command down
end tell
end tell
end tell

VideoBeagle
Jun 3, 2012, 03:52 PM
Thanks! Your code helped me get it cobbled together.

What it's for is a folder action that:

When I drop a text file into a specific folder in my drop box, a stickie note of random color is made of the contents of that text file.

(So that I can leave myself notes to do at home when I'm at work, for example)

Here's what I have and it's working
on adding folder items to this_folder after receiving these_items
if these_items is not ".DS_Store" then
set theFile to these_items

open for access theFile
set fileContents to (read theFile)
close access theFile
--set test to "abc"
--display dialog fileContents
set the clipboard to fileContents
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 0.5
keystroke "n" using command down
keystroke "v" using command down
end tell
set the_colors_list to {"blue", "gray", "pink", "yellow", "green", "purple"}
set rand_color to some item of the_colors_list
click menu item rand_color of menu "Color" of menu bar 1

end tell
end tell
end if
end adding folder items to

I would like to add for it to move the stickie note to a random location on the screen, as right now it stacks everything on the side of the screen. I can do the random, but not sure if the windows can move (my tries so far show "no")
http://i.imgur.com/aPCjIs.jpg (http://imgur.com/aPCjI)

And one problem is that it doesn't seem to work if there's 2+ text files put in the folder at the same time. This is an unlikely event to happen, but I'm hoping to figure out how to make it work.

kryten2
Jun 3, 2012, 09:02 PM
This script is tested with 3 text files :

on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
-- insert actions here
if this_item is not ".DS_Store" then
set the_file to (read this_item)
set the clipboard to the_file
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 1
keystroke "n" using command down
keystroke "v" using command down
end tell
set the_colors_list to {"blue", "gray", "pink", "yellow", "green", "purple"}
set rand_color to some item of the_colors_list
click menu item rand_color of menu "Color" of menu bar 1
end tell
end tell
end if
end repeat
end adding folder items to


If you make 2 stickies you can set their position with this script :

tell application "System Events"
tell process "Stickies"
tell window 1
set position to {563, 423}
end tell
tell window 2
set position to {650, 750}
end tell
end tell
end tell

I tried to merge this with the first script but failed. Perhaps you have more luck and can cobble it together again.

VideoBeagle
Jun 4, 2012, 09:08 AM
This script is tested with 3 text files :

on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
-- insert actions here
if this_item is not ".DS_Store" then
set the_file to (read this_item)
set the clipboard to the_file
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 1
keystroke "n" using command down
keystroke "v" using command down
end tell
set the_colors_list to {"blue", "gray", "pink", "yellow", "green", "purple"}
set rand_color to some item of the_colors_list
click menu item rand_color of menu "Color" of menu bar 1
end tell
end tell
end if
end repeat
end adding folder items to


If you make 2 stickies you can set their position with this script :

tell application "System Events"
tell process "Stickies"
tell window 1
set position to {563, 423}
end tell
tell window 2
set position to {650, 750}
end tell
end tell
end tell

I tried to merge this with the first script but failed. Perhaps you have more luck and can cobble it together again.

Hey thanks.. I'll try 'em tonight.. as I was typing the report up last night, I was thinking I'd try a repeat and see if that would fix it.

On the move I had tried under the tell stickies "set position of window 1 to {x,x}, but it didn't work..now I see why. Very difficult to find how this syntax works in a google search :(

VideoBeagle
Jun 5, 2012, 12:23 AM
I was able to successfully (and easily) infuse Kryten2's code into the script. I've tested it with 6 files and it worked.

A test from work today with the previous code failed, which doesn't make any sense as it works from website tests, from other computers in my home, having drop box delete the test files then restore them, and even from a friend dropping into a shared folder a state away. I'll test the new code in the morning.

I chose the range of width/height so that the script can be used unaltered by most people with modern set ups. Despite the random generation of the location, the script appears to like to bunch stickies near each other, but assuming Applescript's random function is more or less "true random", that's just the way it goes.

on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
-- insert actions here
if this_item is not ".DS_Store" then
set xx to random number from 15 to 1000
set yy to random number from 30 to 800
set the_file to (read this_item)
set the clipboard to the_file
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 1
keystroke "n" using command down
keystroke "v" using command down
set position to {xx, yy}
end tell
set the_colors_list to {"blue", "gray", "pink", "yellow", "green", "purple"}
set rand_color to some item of the_colors_list
click menu item rand_color of menu "Color" of menu bar 1
end tell
end tell
end if
end repeat
end adding folder items to

Unless anyone (well, Kryton2 :p) has any other suggestions, I'll clean up the script and call it good to go!

Edit: I'm thinking that instead of choosing a random number out of hundreds of numbers, for the positions, changing it to choosing from an array of just 100,200,300,400 etc for each coordinate. which should put the notes essentially in some position on a grid. Not sure if this would help with the bunching up issue or speed things or whatnot..

kryten2
Jun 5, 2012, 10:45 AM
Perhaps you can do something with this :

tell application "System Events"
tell process "Stickies"
set theWindowList to every window
set thePositionList to {}
repeat with i from 1 to number of items in theWindowList
set aWindow to item i of theWindowList
set end of thePositionList to position of aWindow
end repeat
end tell
end tell
thePositionList

VideoBeagle
Jun 5, 2012, 11:57 AM
I'm not sure what that code does..(I can't test it til I get home).

It seems to just make an array of the positions of the notes?

Ultimately, for my use at least, I can't imagine there ever being more than 3 notes at a time, and usually not even that. This is really for items I suddenly think of when away that I must take care of when I get home, like "Oh! Must remember to pay the cable bill!" "Mom's Birthday! Call her!" rather than a lengthy to do list.

(Obviously, I'd like to make it robust enough for it to work for other folk's needs)

My eventual plan is to change the triggering mechanism, so that the notes come from like simplenote/notational velocity or something, but that's down the road.

VideoBeagle
Jun 8, 2012, 12:17 AM
OK, new problem has croped up,
Using a service that puts a text of an email in drop bx, I can use this system from my ipad....BUT the text files all have same name, so the action doesn't trigger.

So i figured having the file deleted before it loops would be a good idea. I tried the code below, and it works for the first iteration..then doesn't..and the script never ends (the 3 beeps to tell me when it does).

Anyone have thoughts?


on adding folder items to this_folder after receiving these_items
delay 30
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
-- insert actions here
if this_item is not ".DS_Store" then
set xx to random number from 15 to 1000
set yy to random number from 30 to 800
set the_file to (read this_item)
set the clipboard to the_file
tell application "Stickies" to activate
tell application "System Events"
tell process "Stickies"
tell window 1
delay 5
keystroke "n" using command down
--delay 3
keystroke "v" using command down
set position to {xx, yy}
end tell
set the_colors_list to {"blue", "gray", "pink", "yellow", "green", "purple"}
set rand_color to some item of the_colors_list
click menu item rand_color of menu "Color" of menu bar 1
end tell
end tell
tell application "Finder"
activate
delete file this_item

end tell
end if
end repeat
beep 3
end adding folder items to

kryten2
Jun 8, 2012, 08:37 AM
BUT the text files all have same name
You can't have files of the same name. See if you can create/put 2 files with the same name into a folder. You can't. Period. Say you have a folder with a file named textfile.txt in it. Try to put textfile.txt in it again. Finder will complain the file already exists and will ask you if you want to replace it. If you copy textfile.txt and paste it into the same folder it will get the name textfile copy.txt, textfile copy 2.txt and so on.

VideoBeagle
Jun 8, 2012, 09:01 AM
You can't have files of the same name. See if you can create/put 2 files with the same name into a folder. You can't. Period. Say you have a folder with a file named textfile.txt in it. Try to put textfile.txt in it again. Finder will complain the file already exists and will ask you if you want to replace it. If you copy textfile.txt and paste it into the same folder it will get the name textfile copy.txt, textfile copy 2.txt and so on.

You can in drop box.

This is what happens:

1) Send email with subject memo
2) sendtodropbox service sends text file of email to drop box folder titled memo.txt
3) memo.txt appears in drop box folder
4) Folder action triggers.
5) send second email with subject memo
6) endtodropbox service sends text file of email to drop box folder titled memo.txt
7) dropbox "updates" pre-existing memo.txt to the new version.
8) Folder Action doesn't trigger.

Thus, the best solution I've come up with is to delete memo.txt after the sticky has been created, thus the code I've tried to add, but so far it only works in a first loop if there's multiple files added at once.

kryten2
Jun 8, 2012, 01:28 PM
Sorry, my bad. I don't use Dropbox and admit it's like Chinese to me. To debug the script I suggest you take a look at Script Debugger. It lets you run your folder action without attaching it to a folder so you can see where or why it errors.

VideoBeagle
Jun 8, 2012, 04:28 PM
Sorry, my bad. I don't use Dropbox and admit it's like Chinese to me. To debug the script I suggest you take a look at Script Debugger. It lets you run your folder action without attaching it to a folder so you can see where or why it errors.

Thanks. I'll look at that. But the code I'm using looks right? It's not doing something like erasing the counting list or anything?

kryten2
Jun 8, 2012, 05:34 PM
Code looks ok. Why don't you try the script by manually feeding it the files that make it error? Comment out the handler and give this a try :

set the_folder to choose folder
tell application "Finder" to set these_items to (entire contents of the_folder) as alias list
-- copy the repeat here
-- For testing use this :
--tell application "Finder" to delete this_item
beep 3

VideoBeagle
Jun 11, 2012, 06:13 PM
Thanks. I'll try that code. (I've taken some days off of beating my head on this).


The Script Debugger you mention...do you mean this? http://www.latenightsw.com/

kryten2
Jun 11, 2012, 06:41 PM
The Script Debugger you mention...do you mean this? http://www.latenightsw.com/

Yep, that's the one.

VideoBeagle
Jun 12, 2012, 01:09 PM
Yep, that's the one.

Thanks. I'll give the trial of the older version (the current is intel only) a shot!

VideoBeagle
Jan 23, 2014, 06:27 PM
NEW YEAR, NEW PROBLEMS:

Maverick's security bs has messed up my script but good.

Because of the new by application accessibility rules, the script wouldn't run..or if it did, it wouldn't finish.

And I couldn't get it to trigger the automatic "Do you want to let this program do it" diaglog.

After pounding my head for a while, I found this page:
http://macosxautomation.com/mavericks/guiscripting/reset.html

Following the instructions, I was able to let System Events have access so it works now.

I imagine there are "good" reasons why this was a bad idea, but my tool works again, so yay!

ArtOfWarfare
Jan 24, 2014, 08:22 AM
I imagine there are "good" reasons why this was a bad idea, but my tool works again, so yay!

No - if you know what your script is doing, then I would say there's no problem giving it additional privileges. If, on the other hand, you simply copy and pasted it from online, or you didn't know what you were doing, or you just downloaded the script, it could have been a malicious script which you obviously wouldn't want to run.