Рутината "on switchDir" има за цел да намери всеки елемент от пътя, подаден като "директория", и да щракне върху него, така че пътят на директорията да се гмурне, за да достигне желаната крайна изходна директория.
При активиране на желания ред с мишката. както обикновено, той трябва да бъде щракнат два пъти, за да го изберете!
Това е кодът само на "on switchDir":
on switchDir(directory, appName, selectDefault, createIt)
local compname, bootvolume
set compname to get computer name of (system info)
set bootvolume to get boot volume of (system info)
if directory is equal to "~" then set directory to system attribute ("HOME")
if directory starts with "~/" then set directory to (system attribute ("HOME")) & text 2 through -1 of (get directory)
if not checkPathExists(directory) then
if createIt then
do shell script ("mkdir -p " & (POSIX path of directory))
else
return false
end if
end if
if directory begins with "/" then
set directory to bootvolume & (get directory)
end if
tell application "System Events" to tell (process "iTunes"'s front window)
delay 1
click pop up button 1 of group 1 -- selects the drop-down box above the directory listing
set max to the count of menu items of menu 1 of pop up button 1 of group 1 -- number of items in the drop-down menu
set ndx to 1
repeat while ndx ≤ max
if the title of group 1's pop up button 1's menu 1's menu item ndx as string is equal to compname then -- found the absolute top-level directory
click group 1's pop up button 1's menu 1's menu item ndx - so choose it and go to next part of navigation
exit repeat
else -- keep looking
set ndx to (get ndx) + 1
end if
end repeat
if ndx > max then error "Never found " & compname
set thePath to every item of my splitString(directory, "/") -- thePath equals every individual folder in the path's name in order
repeat with dir in thePath
set max to the (count of rows in outline 1 of scroll area 1 of splitter group 1 of group 1) -- max equals the number of rows in the "directory contents" list box
log max
set ndx to 2 -- row 1 is just the colum titles
repeat while ndx ≤ max
log the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string
log the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string is equal to dir as string
if the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string is equal to dir as string then -- this is the row we want!
log "found " & dir & " at row " & ndx as string
select row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 -- included to make sure the reference in the "click" statement is correct
click row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 -- supposed to "click" the desired folder name to choose it and dive deeper, BUT IT DOESN'T ACTUALLY DO THAT!
exit repeat -- apparently never executed because the loop keeps going past finding the desired directory's name and crashes at the first blank row!
else
set ndx to (get ndx) + 1
end if
if ndx > max then error "Never found " & dir
end repeat
end repeat
end tell
error "success"
return true
end switchDir
Грешката възниква в „on switchDir“, останалата част от кода е включена само така, че кодът да се изпълни под „Редактор на скриптове“. И двата „Щракнете ред ndx . . . .“ и операторът "exit repeat" след него очевидно никога не се изпълняват, тъй като цикълът продължава да работи след регистриране на "found". . . . и следователно се срива на първия празен ред в списъчното поле. Намереното (с кавичките) може да се използва като дума за търсене за намиране на кода.
Следва пълният, изпълняваем код на приложението:
on splitString(theString, theDelimiter)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to every text item of theString
set AppleScript's text item delimiters to oldDelimiters
return theArray
end splitString
on checkPathExists(thePath)
if thePath is equal to "~" then set thePath to system attribute ("HOME")
if thePath starts with "~/" then set thePath to (system attribute ("HOME")) & text 2 through -1 of (get thePath)
try
POSIX file thePath as alias
return true
on error
return false
end try
end checkPathExists
on switchDir(directory, appName, selectDefault, createIt)
local compname, bootvolume
set compname to get computer name of (system info)
set bootvolume to get boot volume of (system info)
if directory is equal to "~" then set directory to system attribute ("HOME")
if directory starts with "~/" then set directory to (system attribute ("HOME")) & text 2 through -1 of (get directory)
if not checkPathExists(directory) then
if createIt then
do shell script ("mkdir -p " & (POSIX path of directory))
else
return false
end if
end if
if directory begins with "/" then
set directory to bootvolume & (get directory)
end if
tell application "System Events" to tell (process "iTunes"'s front window)
delay 1
click pop up button 1 of group 1 -- selects the drop-down box above the directory listing
set max to the count of menu items of menu 1 of pop up button 1 of group 1 -- number of items in the drop-down menu
set ndx to 1
repeat while ndx ≤ max
if the title of group 1's pop up button 1's menu 1's menu item ndx as string is equal to compname then -- found the absolute top-level directory
click group 1's pop up button 1's menu 1's menu item ndx - so choose it and go to next part of navigation
exit repeat
else -- keep looking
set ndx to (get ndx) + 1
end if
end repeat
if ndx > max then error "Never found " & compname
set thePath to every item of my splitString(directory, "/") -- thePath equals every individual folder in the path's name in order
repeat with dir in thePath
set max to the (count of rows in outline 1 of scroll area 1 of splitter group 1 of group 1) -- max equals the number of rows in the "directory contents" list box
log max
set ndx to 2 -- row 1 is just the colum titles
repeat while ndx ≤ max
log the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string
log the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string is equal to dir as string
if the value of text field 1 of UI element 1 of row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 as string is equal to dir as string then -- this is the row we want!
log "found " & dir & " at row " & ndx as string
select row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 -- included to make sure the reference in the "click" statement is correct
click row ndx of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 -- supposed to "click" the desired folder name to choose it and dive deeper, BUT IT DOESN'T ACTUALLY DO THAT!
exit repeat -- apparently never executed because the loop keeps going past finding the desired directory's name and crashes at the first blank row!
else
set ndx to (get ndx) + 1
end if
if ndx > max then error "Never found " & dir
end repeat
end repeat
end tell
error "success"
return true
end switchDir
set directory to "/Users/bryandunphy/Music"
try
tell application "iTunes" to quit
end try
tell application "System Events"
key down option
tell application "iTunes" to activate
key up option
repeat until process "iTunes" exists
delay 0.5
end repeat
click process "iTunes"'s window 1's button 2
my switchDir(directory, "iTunes", false, true)
delay 2
set libraryName to value of text field "Save As:" of window "New iTunes Library" of process "iTunes"
delay 2
click process "iTunes"'s window "New iTunes Library"'s button "Save"
end tell
return libraryName
Всяка идея за причината за грешката и/или как да я поправите ще бъде високо оценена.
log "found"
иselect row
надclick
работят, ноclick
иexit repeat
не се спазват? Като за начало, не мисля, че имате нужда отas string
част отlog "found " & dir & " at row " & ndx as string
. Вече свързвате низ. - person pipwerks   schedule 16.03.2017row
или защо искате да щракнете върху него. Възможно е да има други начини за обработка на действието, като например натискане на клавиш. Но първият ред за отстраняване на неизправности е да опростите възможно най-много - публикацията ви по-горе е много гъста, без обяснение какво се случва (напр.set max to the count of menu items of menu 1 of pop up button 1 of group 1
). Можете ли да премахнете скрипта си, за да изолирате счупенияrepeat
? - person pipwerks   schedule 16.03.2017log "found " & dir & " at row " & ndx as string
? Показва ли се както очаквате? - person pipwerks   schedule 16.03.2017click pop up button 1 of group 1
в Сиера с най-новата версия на iTunes. Освен това вместо двойно щракване, опитайте CMD-okeystroke o using {command down}
- person pipwerks   schedule 16.03.2017