Check if Key is down?
Check if Key is down?
- Subject: Check if Key is down?
- From: Ivan <email@hidden>
- Date: Mon, 24 Sep 2001 19:21:10 -0600
First I would like to say thanks to all for your great pointers. I have
been eavesdropping for a little over a week trying to get my questions
answered without saying anything. It's worked wonderfully! With your help
I was able to create File++ Beta for my father. There are a few things
that I would like to ask though:
1. My father wants a script that will open a file with a very specific name
(003 for example). When run with the option key held down he would like it
to increment the file to be opened (to 004 for this example). Is there any
way to check if a key (not necessarily the Option key) is held down-either
when the script is run or anytime therein? The solution I have right now
is that a dialog box opens up with a default value of incrementing the file
name. It gives up after one second, opening the current file. This works
fairly well, but is far from elegant. Is there a better way? I could
obtain any free OSAXen though I have none now. It could certainly use some
revision.
2. When I need to create a new file I copy a template file (set by the
user) and then change the name. It's a pain keeping track of the path and
name etc. Is there an easier way to do this? OSAXen?
if (TempFolder as text = myFolder as text) then
duplicate file Template
set name of file ((Template as text) & " copy") to (genName & newName)
else
copy file Template to folder myFolder -- & genName & newName)
set name of file (myFolder & (get name of Template) as text) to (genName &
newName)
end if
What I would like is something like this:
(Tell app "Finder" to) copy file Template to myFolder with name newName
PS. He wants this because Nisus Writer can use the name of the file in the
document. He then uses this as the number of the newsletter that he
publishes.
Thanks for your patience,
Ivan Andrus
(gVol-The Flying Cat)
Full source given below. Comments on any part are welcome. I really don't
expect you to try and read this unless you are slightly masochistic. There
are some _very_ long lines so watch the wraps. My next project might be
the email formatter suggested by has. Not that I could do it but a little
experiece (pronounced "Bang head on wall") never hurt any one.
*******
property genName : "Thor " --Generic Name
property curName : "000"
property Template : "1-Winky:-Documents:ThorGazette:Thor 000"
property TempFolder : "1-Winky:-Documents:ThorGazette:"
property myFolder : "1-Winky:-Documents:ThorGazette:"
property time_out : 1
property dgt : 3 --number of digits in name
property padding : "000"
on run
set promptText to "What would you like to do?"
set myButtons to {"Open Current", "Update"}
set theResult to (display dialog promptText buttons myButtons default
button "Update" with icon note giving up after time_out)
set input to button returned of the theResult
set gaveUp to gave up of the theResult
if (gaveUp = true or input = "Open Current") then
tell application "Finder" to open file (myFolder & genName & curName)
else
Reassign()
end if
end run
on Reassign()
set newName to 1 + (curName as number)
set newName to (padding & newName)
set i to number of items in newName
--There must be a simpler way to do this
set newName to (characters (i - dgt + 1) through i of newName) as text
set newFile to (myFolder & genName & newName) as text
tell application "Finder"
if exists file (newFile as text) then
set promptText to ("Would you like to use " & genName & newFile & return
& " as the current file?")
set myButtons to {"Cancel", "Set Current File", "OK"}
set input to button returned of (display dialog promptText buttons
myButtons default button 3)
if input = "Set Current File" then SetFile()
else
set promptText to (newFile & " does not exist." & return & " Would you
like to create it?")
set myButtons to {"Cancel", "Set Current File", "OK"}
set input to button returned of (display dialog (promptText as text)
buttons myButtons default button 3)
if input = "Set Current File" then SetFile() of me
if input = "OK" then
if not (exists file Template) then
display dialog "No Template!"
--set template to Find file "Give me a Template!" ( I'm still working
on this
return
end if
if (TempFolder as text = myFolder as text) then
duplicate file Template
set name of file ((Template as text) & " copy") to (genName & newName)
else
copy file Template to folder myFolder
set name of file (myFolder & (get name of Template) as text) to
(genName & newName)
end if
--set locked of newFile as file to false
--set stationery of newFile as file to false
end if
end if
if input = "OK" then
set curName to newName
open file newFile
end if
end tell
end Reassign
on SetFile()
set myResult to display dialog "Current File number is:" default answer
curName buttons {"Cancel", "Set # of Digits", "OK"} default button 3
if button returned of myResult is not "Cancel" then
set curName to text returned of my result --ErrCheck Needed
if button returned of myResult is "Set # of Digits" then
set myResult to (display dialog "Number of Digits" default answer dgt
default button 2)
if button returned of myResult is not "Cancel" then
set dgt to text returned of myResult as number
set padding to ""
repeat dgt times
set padding to padding & "0"
end repeat
end if
end if
end if
end SetFile
on open x
tell application "Finder"
if (count items in x) - 1 then
display dialog "I can only watch one folder at a time. Please try
again." buttons "OK" default button 1
return
end if
set x to item 1 of x
if last character of (x as text) is ":" then --it's a folder
if button returned of (display dialog "This folder" & return & x &
return & " will be used as my working folder") is "OK" then set myFolder to x
else --TEMPLATE OR GENERIC
set newGen to (get name of file x) as text
set myResult to (display dialog "Use file " & newGen & " as:" buttons
{"Nothing", "Base Name", "Template"} default button 3 with icon note)
if button returned of myResult is "Nothing" then return
if button returned of myResult is "Template" then set Template to x
set TempFolder to folder of file Template
-- display dialog ((TempFolder as text) & return & Template as text)
set myResult to (display dialog "Please enter the base name (to be
included at the beginning of all files) Don't forget spaces." default
answer newGen default button 2 with icon note)
if button returned of myResult is "OK" then set genName to text returned
of myResult
end if
end tell
end open