Applescripting stickies application?

Discussion in 'Mac Programming' started by VideoBeagle, May 29, 2012.

  1. macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    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?
     
  2. kryten2, May 29, 2012
    Last edited: May 29, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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 :

    Code:
    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 :

    Code:
    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
     
  3. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #3
    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
    Code:
    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")
    [​IMG]

    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.
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    This script is tested with 3 text files :

    Code:
    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 :

    Code:
    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.
     
  5. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #5
    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 :(
     
  6. VideoBeagle, Jun 4, 2012
    Last edited: Jun 5, 2012

    thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #6
    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.

    Code:
    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..
     
  7. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    Perhaps you can do something with this :

    Code:
    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
     

    Attached Files:

  8. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #8
    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.
     
  9. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #9
    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?


    Code:
    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
    			[COLOR="red"]tell application "Finder"
    				activate
    				delete file this_item
    				
    			end tell[/COLOR]
    		end if
    	end repeat
    	beep 3
    end adding folder items to
     
  10. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    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.
     
  11. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #11
    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.
     
  12. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #12
    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.
     
  13. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #13
    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?
     
  14. kryten2, Jun 8, 2012
    Last edited: Jun 8, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #14
    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 :

    Code:
    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
     
  15. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #15
    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/
     
  16. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #16
  17. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #17
    Thanks. I'll give the trial of the older version (the current is intel only) a shot!
     
  18. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #18
    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!
     
  19. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #19
    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.
     
  20. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #20
    So, i just realized that my script no longer moves the sticky notes into a new position.

    I wanted to change the size of the field for the iMac screen, but they don't move at all when they used to with no issue.

    It's the bolded line that's having the issue.
    (the iMac recently had a new image put on it..so I'm thinking it's a gui scripting security thing, maybe?)


    Code:
    on adding folder items to this_folder after receiving these_items
    	--delay 5
    	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
    			delay 1
    			tell application "Stickies" to activate
    			tell application "System Events"
    				tell process "Stickies"
    					tell window 1
    						delay 1
    						keystroke "n" using command down
    						--delay 3
    						keystroke "v" using command down
    				[B]		set position to {xx, yy}[/B]
    					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
    			--beep 2
    			--end tell
    		end if
    	end repeat
    	beep 3
    end adding folder items to
    
     
  21. thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #21
    And yep, applying the January fix (weird that it was different symptoms) and rebooting, and good to go.
     

Share This Page