في Applescript، كيف يمكنني معرفة ما إذا تم تحديد/تركيز عنصر القائمة؟
-
09-06-2019 - |
سؤال
لدي برنامج نصي لنظام التشغيل OS X 10.5 يركز على مربع البحث في قائمة التعليمات لأي تطبيق.لدي مجموعة مفاتيح، وكما هو الحال مع Spotlight، أريد تشغيله عند تشغيل البرنامج النصي.لذا، أريد اكتشاف ما إذا كان مربع البحث مركّزًا بالفعل على الكتابة، وإذا كان الأمر كذلك، فاكتب Esc بدلاً من النقر فوق قائمة "تعليمات".
وهذا هو السيناريو كما هو الآن:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
click helpMenuItem
end tell
end tell
وأنا أفكر في شيء مثل هذا:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
set searchBox to menu item 1 of menu of helpMenuItem
if (searchBox's focused) = true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
...لكني أحصل على هذا الخطأ:
لا يمكن التركيز على {menu item 1 of Menu "Help" of Menu bar item "Help" of Menu bar 1 of application application "Script Editor" of application "System Events"}.
فهل هناك طريقة يمكنني من خلالها جعل البرنامج النصي الخاص بي يكتشف ما إذا كان مربع البحث مركّزًا بالفعل؟
لقد حلت مشكلتي عن طريق العمل حوله.ما زلت لا أعرف كيفية التحقق من تحديد عنصر القائمة، لذلك سأترك هذا الموضوع مفتوحًا.
المحلول
باستخدام /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app، يمكنك استخدام نظام إمكانية الوصول المدمج للاطلاع على خصائص عنصر واجهة المستخدم أسفل الماوس.خذ ملاحظة خاصة بالإجراء cmd-F7 لقفل التركيز على عنصر وزر التحديث.من المؤسف أن أسماء العناصر والخصائص لا تتطابق بشكل مباشر مع تلك الموجودة في مجموعة البرامج النصية، ولكن يمكنك إلقاء نظرة على قاموس أحداث النظام أو عادةً تخمين المصطلحات الصحيحة.
باستخدام هذا يمكنك تحديد شيئين.لأول مرة focused
الملكية ليست على menu item
, ، بل هناك text field
في حدود menu item
هذا يركز.ثانيا، يحتوي عنصر القائمة على selected
ملكية.
وبهذا توصلت إلى:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
-- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn't good at resolving after the fact.
set searchBox to a reference to menu item 1 of menu of helpMenuItem
set searchField to a reference to text field 1 of searchBox
if searchField's focused is true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
على الرغم من أن هذا لا يزال لا يعمل.الحدث الرئيسي لا يتم إطلاقه بقدر ما أستطيع أن أقول، لذلك ربما لا يزال هناك شيء غريب مع focused
خاصية في حقل النص
على أية حال، الخاص بك click
مرة أخرى يبدو الحل أسهل بكثير.
نصائح أخرى
المدمج في الاختصار الرئيسي كمد-؟ (كمد-التحول-/) يتصرف بالفعل مثل هذا.فهو ينقل التركيز الرئيسي إلى حقل البحث في قائمة المساعدة إذا لم يكن مركزًا بالفعل، ويتجاهل القائمة.
تحتاج إلى استخدام السمة AXMenuItemMarkChar
.
مثال:
tell application "System Events"
tell process "Cisco Jabber"
set X to (value of attribute "AXMenuItemMarkChar" of menu item "Available" of menu "Status" of menu item "Status" of menu "File" of menu bar item "File" of menu bar 1) is "✓" -- check if Status is "Availible"
end tell
end tell
إذا تم تحديد عنصر القائمة، فإن القيمة المرجعة هي ✓
, ، وإلا فهو كذلك missing value
.
ملحوظة:يعمل هذا الاختبار فقط إذا كان التطبيق الذي يتم فحص قوائمه موجودًا حاليًا في المقدمة.
لقد واجهت للتو الحاجة إلى القيام بذلك بنفسي لبعض معالجة الملفات في Illustrator.
وهنا ما توصلت إليه:
tell application "Adobe Illustrator"
activate
tell application "System Events"
tell process "Illustrator"
set frontmost to true
set activeMenuItem to enabled of menu item "Unlock All" of menu "Object" of menu bar item "Object" of menu bar 1
if activeMenuItem is true then
tell me to beep 3
else
tell me to beep 2
end if
end tell
end tell
end tell
منتهي.
لقد نجح هذا دون أي مشكلة ويمكن استخدامه لتكرار الملف.ربما سأضطر إلى القيام بذلك عدة مرات في أتمتة المستقبل.
حظ سعيد!